javascript - "this" variable not working when assigning JSONP array to AngularJS controller -


i have rest service call in angular controller via jsonp. want save returned array variable.

here doing:

this.testtherest = function() {     $http.jsonp('myrestservice', {             params: {                 callback: 'json_callback',                 format: 'json'             }         })             .success(function (data) {                 this.testlist = data.noparamsresult;                 console.log(this.testlist);             })             .error(function (data) {                 alert(data);             });          console.log(this.testlist); } 

i have defined testlist variable in controller beforehand:

this.testlist = []; 

note 2 console output calls. problem is, first 1 shows correct array:

array [ object, object ] 

but second 1 shows empty array (probably 1 initialization):

array [  ] 

what have keep returned objects in array?

thanks in advance,
frank

this scoping issue. try assigning outer this variable (e.g. that), using variable instead of this.

var =  that.testtherest = function() {     $http.jsonp('myrestservice', {         params: {             callback: 'json_callback',             format: 'json'         }     })     .success(function (data) {         that.testlist = data.noparamsresult;         console.log(that.testlist);     })     .error(function (data) {         alert(data);     }); } 

Comments

Popular posts from this blog

Android : Making Listview full screen -

javascript - Parse JSON from the body of the POST -

javascript - Chrome Extension: Interacting with iframe embedded within popup -