AngularJs controller, wait for service $http result -


Before the rest controller, I want to wait for a service $ http result. I have some hard work JSON And it is visible in the controller, but as soon as I try to populate my data with a $ http request, the controller is not waiting for it and just walks through the rest of the code. Chrome is showing the expected result for the Network tab request from webdeveloper.

I already have the following:

  var ListerApp = angular.module ('ListerApp', ['ListerApfilter', 'Shared Factor App', ' Shared Services', 'ListerApus Controller', 'Infinite Scroll', 'Connection-Inview', 'NGRUTU']); ListerApp.config (['$ routeProvider', '$ httpProvider', function ($ route provider, $ location provider) {$ routeProvider. When ('/ list', {templateUrl: '/assets/services/partials/list.html' Controller: 'Lister Ctrl', Resolve: {'sharedServiceAppData': function ($ sharedServices) {// SharedFactoryAppData will be injectable in your controller too, if you do not want it, then you make a new promise with $ q service return $ Shared Services.Promise ();}}}})}}); ('$ Sharedservices', ['$ http', '$ rootscope', '$ q', function ($ http, $ rootscope, $ q) {var myData = null; return} {promise: work () {if (! MyData) {$ http ({method: 'GET', url: '/ shop / api / json', parameters: {end: $ rootScope.endmarkerSyncDataset, page: 1, Per_page: $ rootScope.itemsPerPage}}) Success (function (Data) {myData = data.data_set; //deferred.resolve(data.data_set);}); // I have tested it completely and it is fine: myData = {Foo: 'bar'};} }, MatchShopData: function () {myData;}};}]); ('$ Scope', '$ rootScope', '$ http', '$ filter', '' $ '', '$ timeout', '$ shared factory', '$ shared services' Function ($ Range, $ Rootscope, $ http, $ Filter, $ Timeout, $ Shared Factory, $ Shared Server) {$ scope.items = $ sharedServices.getShopData (); Console.log ($ scope.items ); // myData -> Null}});  

I It has done so many times that I usually need to ensure that what I do in my resolution for route provider That is, you should return all the promises in a proper way for the path change to wait on the resolution properties.

  $ routeProvider. When ('/ path', {templateUrl: 'some / Path / to / template.html ', Controller:' myCtrl ', solve: {' MyData ': [' mySrvc ', function (mySrvc) {back mySrvc.someMethod (). Then (function (response) {return response. Data;}, function () {return {};});}]}});  

Then service:

  Angular Module ('myapp.services'). Factory ('mySrvc', ['$ http', function ($ Http) {var _url = 'https://someurl.com/path/to/api'; return {someMethod: function (vars) {if (angular. Isdefined (wars)} {return $ http.post (_url, $ .prem (wars), {header: {'content-type': 'app / x- www.form-urlencoded'}})}} {return $ Http.get (_url)}}}}}}]);  

Then in the controller you need to inject the solution parameters at the end of the parameter list for the controller.

  angular.module ('myApp Controller'). Controller ('myCtrl', ['$ scope', 'myData', function ($ scope, myData) {}); Return  

Try again to return, promise $ http and then data.data_set value in the success function of your service.


Comments

Popular posts from this blog

winforms - C# Form - Property Change -

java - Messages from .properties file do not display UTF-8 characters -

javascript - amcharts makechart not working -