i have following new service:
var signatureservice = function ($scope) { this.announce = function () { alert($scope.specificname); } }; signatureservice.$inject = ['$scope'];
this declared app follows:
myangularapp.service('signatureservice', signatureservice);
several other services added app in same way, , seem work ok. have controller declared , defined follows:
myangularapp.controller('mycontroller', mycontroller); ... var mycontroller = function ($scope, service1, service2, $location, $modal, signatureservice) { ... } mycontroller.$inject = ['$scope', 'service1', 'service2', '$location', '$modal', 'signatureservice'];
i using unconvcentionaly manner of defining servivce , injecting standard in app working on, works existing services, , prefer slot mine in per standard.
when controller loads, [$injector:unpr]
in browser console, error info:
$injector/unpr?p0=$scopeprovider <- $scope <- signatureservice
you can't inject $scope
custom service. doesn't make sense since signatureservice
can injected anywhere including other services , other controlles. $scope
supposed if inject 2 nested controllers, 1 should injected?
scope object ($scope
) associated dom node, attached it. that's why see $scope
in controllers , directives. , reason why can't have in service: services not related specific dom elements. of course can inject $rootscope
unlikely need in question.
summary: $scope
created $rootscope
, injected in necessary controllers, can't injected custom service.
upd. based on comments want use service define reusable controller methods. in case go call mixin approach. define methods in service , mix them in necessary controllers.
app.service('controllermixin', function() { this.getname = function() { alert(this.name); }; });
and extend controller scope angular.extend
:
app.controller('onecontroller', function($scope, controllermixin) { angular.extend($scope, controllermixin); // define controller specific methods }); app.controller('twocontroller', function($scope, controllermixin) { angular.extend($scope, controllermixin); // define controller specific methods });
this pretty effective, because controllermixin
doesn't have notion of $scope
whatsoever, when mixed controller refer scope this
. service doesn't change if prefer use controlleras syntax, extend this
:
angular.extend(this, controllermixin);
Comments
Post a Comment