angularjs - ng-change function gets run on page load before data loads? -


one of controller functions gets run before data loads.

the html:

<div ng-show="item.name">     <input ng-change="dochange()" ng-model="item.name"> </div> 

my controller:

$scope.item = { name: 'bob' }; $scope.other = {};  $scope.dochange = function() {     $scope.item = $scope.other['test'].name }  // load data now! myservice.getdata().success(function(newdata) {     $scope.item = newdata.item;     $scope.other = newdata.other; }); 

my service:

app.factory("myservice", function($http) {     return {         getdata: function() {             return $http.get("/data");         }     } }); 

this results in

typeerror: cannot read property 'name' of undefined

because dochange() gets executed before service has loaded data. i'm not sure why happens. shouldn't dochange run when input has changed, yet it's getting run on page load.

change

$scope.dochange() {   $scope.item = $scope.other['test'].name } 

to

$scope.dochange = function() {   $scope.item = $scope.other['test'].name } 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -