javascript - AngularJs $scope doesn't update after a GET request on a factory -


I am trying AngularJS for the experimental project and I have come up with this problem. I want to display a list of items in my HTML

index.html

  & lt; H1 & gt; Some lists & lt; / H1> & Lt; Div ng-controller = "datlist" & gt; & Lt; Div ng-repeat = "item in item" & gt; & Lt; Div & gt; Item Details: {{item.description}} & lt; / Div & gt; & Lt; Div & gt; Item name: {{item.name}} & lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; First of all, I was using a simple controller so that I could get the information and update the view using this:  

The controller J (Basic) Function Datelist ($ Range, $ http) {$ http ({method: 'GET', url: 'http: // localhost: 61686 / getdatlist? Format = json', header : {'Access-Control-Permission-Origin': 'Localhost: *'}}). Success (function (data, status, header, config) {$ scope.items = data.itemsToReturn; console.log (data);}) error (function (data, status, header, config) {console.log ("unsuccessful ");}); }

This was working very well and I could get the list of items. However, to make a single request and to use a factory, I can tie it to a $ radius by changing my structure. It does not work. I tried to make many changes to the $ watch, but I could not update it to $ scope. I got some information about $, but actually how can I not use it.

Controller. JS (New)

  var DatModule = Angular Module ('datModule', []); DatModule.controller ('datlist', function ($ scope, datfactory) {$ scope.items = datfactory.getlist (); $ scope. $ Watch ($ scope.items, $ scope.items = datfactory.getlist ()); }); DatModule.factory ('datfactory', function ($ http) {var factory = {}; factory .getlist = function () {$ http ({method: 'GET', url: 'http: // localhost: 61686 / getdatlist (Format, Data, Status, Header, Config) {console.log (data.itemsToReturn);) // I get the right things, OK, OK Return data here. Items returned;} Error (function (data, Position, header, config) {console.log ("unsuccessful");})}} return factory;});  

Any ideas about this would be great PS: I have got many posts on this issue, but none of them helps me in finding complete solutions.

Thanks

It is unwise to use that watch

  datModule.factory ('datfactory', function ($ http, $ q) {this.getlist = function () {return $ http.get ('Http: / / Localhost: 61686 / getdatlist = format = json ', {' access-control-allow-origin ':' localhost: * '}). Then (function (feedback) {console.log (response); // Get it all right, okay, send feedback here. Data.itemsotrrrrr;})}} return to;}); DatModule.controller ('datlist', function ($ radius, datfactory) {datfactory.getlist (). Then (function (arrItems) {$ scope.items = arrItems;});});  

In this way you use the promises for the asynchronous case

update (15.01.2015): still sleek!


Comments