angularjs - Injecting a Angular service into controller results in $injector:unpr error (using AngularBooter) -
i using angularbooter angular in app.
here code:
emp.boot(); emp.services.socialservice = ['$http', '$q', function ($http, $q) { var deferred = $q.defer(); $http.get('/api/feed').then(function (data) { deferred.resolve(data); }); this.getstatuses = function () { return deferred.promise; } }]; emp.controllers.statusescontroller = ['$scope','socialservice', '$interval', function($scope, socialservice, $interval) { var statuses = socialservice.getstatuses(); statuses.then(function(data) { $scope.statuses = data; console.log($scope.statuses); }); }];
from understand error: error: [$injector:unpr]
sign service isn't being registered or implemented correctly, have tried few things keep running same problem. ideas.
two problems:
1) booting before register stuff (at least in code provided). emp.boot()
right @ bottom after has been defined.
2) service not returning anything. instance, if add return {};
@ bottom of service function, should magically not throw error anymore, want return more interesting that.
you can see in action uncommenting line returns object , see angular start working again. http://codepen.io/alex-wilmer/pen/qbwngz?editors=101
i imagine want this:
emp.services.socialservice = ['$http', '$q', function ($http, $q) { var deferred = $q.defer(); $http.get('/api/feed').then(function (data) { deferred.resolve(data); }); return { getstatuses = function () { // don't mix brace placement! return deferred.promise; } }; }];
Comments
Post a Comment