I am new to AngularJS all over the world and how it works, however I try my best to make it work as expected. I know this is due to the way I use $http.get() and trying to assign variables back to my controller, but I just can't figure it out.
Using $scope instead of this , I can make it work, however, if possible, I would prefer to use this , so I can use "controller as"
the 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'}];}); });
In this example, I cannot access the results from the $http.get request from the supplierList in the HTML page (ie {{ supplier.supplierList[0].supplier_name }} does not display any results)
I know that if I change the controller to $scope , I will get access to this data (although I do not use the same format as above), and I also know that the data is populated using console.log(this.supplierList) inside a .success call.
I also know that the reason it doesnβt work is because the this context is changed from inside the controller by calling $http.get .
So my question is this: how do you access the results of calling $ http.xxx using this instead of scope ? I read several different sources, but most of them talk about using $scope and promises. I did not find this cover using this (or declaring it with var supplier = this ). Any help would be greatly appreciated.
Thanks,
Doug
source share