javascript - AngularJS, $http.get() and "controller as" -


i'm pretty new whole angularjs world , how works, struggling working expected. know way using $http.get() , trying assign variables controller, can't figure out.

using $scope instead of this can working, if possible, i'd prefer using this can use "controller as"

code:

app.controller('ctrlsuppliers', function($http){      this.supplierlist = {};      $http.get("http://some_url_here")             .success(function(response) {  this.supplierlist = response.records;})             .error(function() { this.supplierlist = [{supplier_id: 0, supplier_name: 'error getting  details'}];}); }); 

from example, cannot access results $http.get request within supplierlist within html page (i.e. {{ supplier.supplierlist[0].supplier_name }} doesnt display results)

i know if change controller $scope can access data (although not using same format above), , know data being populated using console.log(this.supplierlist) inside .success call.

i know reason not working because context of this changes within controller within $http.get call.

so question this: how access results $http.xxx call using this instead of scope? have read few different sources on it, talk using $scope , promises. i've not found cover using this (or declaring var supplier = this). appreciated.

thanks,

always store variable reference this don't have context issues, use variable instead of this throughout controller

app.controller('ctrlsuppliers', function($http){     var vm = this;     // can forget using "this" , use variable instead     vm.supplierlist = {};      $http.get("http://some_url_here") .success(function(response) {          // no context issues since "vm" in scope            vm.supplierlist = response.records;     });                }); 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -