angularjs - How to access the $scope where the factory was injected? -
say have factory named myfactory
, inject several controllers. how can access controllers scope inside factory?
the way think of on how follows:
app.factory('myfactory', function() { return function($scope) { mypublicfunc: function() { $scope.$on('$destroy', function() { ... }); } } }); app.controller('mycontroller1', ['$scope', myfactory', function($scope, myfactory) { var factory = new myfactory($scope); factory.mypublicfunc(); });
but there other way can return { }
instead of function($scope) { }
in myfactory
, use factory directly (myfactory.mypublicfunc
) instead of having create new instance new
keyword and still access each controller's $scope?
if use .service
instead of .factory
have singleton instance in controller once inject via di
and can example
module.service('myservice', function() { return { mypublicfn: function() {} } });
and
module.controller('myctrl', function(myservice, $scope) { $scope.publicfn = myservice.mypublicfn; });
Comments
Post a Comment