0

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>
6
  • Are you sure your success handler is being called? Commented Mar 12, 2019 at 10:50
  • Yes able to get debugger and alert msg. Commented Mar 12, 2019 at 11:02
  • You probably need to define your variables outside of the AJAX request, and then you can update them in the success handler. My guess is when the template is loading, those variables don't exist. Commented Mar 12, 2019 at 11:04
  • Updated outside ajax req but still same issue exists. Commented Mar 12, 2019 at 11:19
  • Try defining your variables with an extra level, something like $scope.This_Controller.mdItemId and $scope.This_Controller.mdFlavorId, and then refer to them within the page as This_Controller.mdItemId and This_Controller.mdFlavorId. Cannot explain why, but this solved a similar problem I had in the past. Also, as suggested by @AndroidNoobie, you should defined them at the controller's level and not inside any function contained there. Commented Mar 12, 2019 at 11:34

1 Answer 1

0

Try like this.

 var itemDtls  = response.data;
 $scope.mdItemId = itemDtls [0].ITEM_ID;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.