角工厂依赖注入 [英] angular factory dependency injection
本文介绍了角工厂依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从移动控制器一些通用的导航格式code到工厂里。
I'm trying to move some generic navigation formatting code from the controller into a factory.
我是否需要注入$范围到我的工厂?我试过六种方式从周日注入$范围,但每次我试过的方法给我的错误。
Do I need to inject $scope into my factory? I've tried six ways from Sunday to inject $scope, but every method I've tried gives me errors.
还是做我的stagesHeight,在工厂stagesWidth变量需要范围界定呢?
Or do my stagesHeight, stagesWidth variables in the factory need scoping at all?
控制器:
angular.module('sysomos.ads').
controller('LinkController', ['$scope', '$state', '$api', 'LinkFactory',
function($scope, $state, $api, LinkFactory) {
console.log(LinkFactory.make(['twitter', 'ad', 'view']));
}
]);
工厂:
angular.module('sysomos.ads').
factory('LinkFactory', function(){
return {
make: function(arrSteps){
$scope.stagesHeight = 30;// what scope does my logic need?
$scope.stagesWidth = 145;
// lots of intervening steps
return arrSteps.join(",");// just return me the array for now
};
}
]);
推荐答案
尝试是这样的:
.factory('LinkFactory', function(){
return {
make: function(arrSteps){
var stagesHeight = 30;// what scope does my logic need?
var stagesWidth = 145;
// lots of intervening steps
return arrSteps.join(",");// just return me the array for now
};
}
然后从CTRL像这样打电话给你的服务/工厂:
Then call your service/factory from your ctrl like this:
$scope.foo.something = LinkFactory.make();
我希望这有助于。
I hope this helps.
这篇关于角工厂依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文