angularjsangularjs-directiveangularjs-scopeangularjs-bindings

AngularJs scope undefined while binding a textbox


Trying to bind data to input control from scope using below code.

var app = angular.module('ng_app', []);

app.controller("ITEM_Ctrl", function ($scope, $http) {
    var PlantId = "abcFC"; $scope.mdItemId; $scope.mdFlavorId; $scope.itemDtls = {};
    $http({
        method: 'GET',
        url: 'http://localhost:12752/api/Item/GetItem',
        params: { Plant_Id: PlantId, Item_Id: '1234567' }
    }).then(function successCallback(response) {
        debugger;
        //  $scope.itemDtls = {};   
alert(response.data);  --> in alert msg as  [object Object]            
    //    $scope.itemDtls = response.data; -->  ReferenceError: itemDtls is not defined
      //  $scope.testmdItemId = itemDtls.ITEM_ID;
        //  $scope.itemDtls =   response.data;
     //   $scope.mdItemId = response.data.ITEM_ID;  --> scope mdItemId undefined
       // $scope.mdItemId = JSON.parse(response.data.ITEM_ID);
       // $scope.mdFlavorId = JSON.parse(response.data.FLAVOR_ID);
      //.mdFlavorId = "test data";  -- > binding successfully
        $scope.itemDtls = response.data;
            $scope.mdItemId = itemDtls.ITEM_ID;
            $scope.mdFlavorId = response.data.FLAVOR_ID;    
    }, function errorCallback(response) {

    });
});

Tried in different ways to bind to input control as above but getting msg as scope undefined.

<body ng-app="ng_app">
    <table ng-controller="ITEM_Ctrl">
        <tr>              
            <td>
                <div>                   
                    <input type="text" id="itemid"  ng-model="mdItemId" />
                </div>
                <div>                 
                    <input type="text" id="flavorid"  ng-value="mdFlavorId" />
                </div>                
            </td>
        </tr>
    </table>

Solution

  • Try like this.

     var itemDtls  = response.data;
     $scope.mdItemId = itemDtls [0].ITEM_ID;