Angularjs Init Service -
I am working on the AngularJs project in which the top navigation items are loaded using the service call
App.service ('menu', function ($ http) {return: function: callback} {$ http.get ('api / menu.json'). Success (function (menu data) {callback (menuData);});}}}); I am using the same as described in my BaseController App.controller ('BaseCtrl' , Function ($ scope, menu) {$ scope.menuData = {}; Menu.get (function (menu data) {$ scope.menuData = menuData;})}} My The menu remains empty for a period of time until the service call is successful and see before you assign any method before assigning the value of $ scope.menuData . Please tell me, I would like to do everything possible You may have tried, but nothing worked: (
PS: I am using AngularJS v1.2.9
Return a promise from your service, and use it in your controller's resolution:
app.service ('menu', function ($ http) {var menuData = null; var promise = $ http.get ('api / menu.json'). Success (function (data) {menuData = data;}); return {getMenuPromise: promise, getMenuData: function () {return menuData;}};}); And in your router definitions:
$ routeProvider Resolve: {'menuData': function (menu) {return Menu.getMenuPromise;}}}) when the ('/ home', {administrator: 'basckton', template url: '../http; home page') < / pre> Now when BaseCtrl is on, the data is there: app.controller ('BaseCtrl', function ($ radius, menu) {$ scope.menuData = MyService.getMenuData ();});
Comments
Post a Comment