Angularjs 控制器内部工厂的不同依赖注入
Angularjs different dependency injection for factories inside controller
假设我有多个工厂,这些工厂内部有相同的函数和变量。例如,
var app = angular.module('app', []);
app.factory('employerFactory', function () {
return {
firstName: 'Kristoffer',
lastName: 'Karlsson',
};
});
app.factory('employeeFactory', function () {
return {
firstName: 'Alice',
lastName: 'Guo',
};
});
现在我有一个这样的控制器:
app.controller('MyController', function($scope, myFactory) {
$scope.firstName = myFactory.firstName;
$scope.lastName = myFactory.lastName;
});
我的问题是如何启动两个控制器,一个作为依赖传入employerFactory,另一个是employeeFactory。我知道 routerProvider 可以帮助为不同的 url 路由不同的控制器。我可以操纵它以将不同的工厂传递给控制器吗?像
$routeProvider.
when('/a', {
templateUrl: 'test.html',
controller: 'MyController' // should get passed factory 'employerFactory'
}).
when('/b', {
templateUrl: 'test.html',
controller: 'MyController' // should get passed factory 'employeeFactory'
});
只需使用一个服务作为一个额外的层来处理切换逻辑,将两个工厂注入其中,然后创建方法来处理通用功能:
app.service('FactoryService', function($scope, employerFactory, employeeFactory) {
var _mode = 0;
var _factories = [employerFactory, employeeFactory];
return {
setMode: function(mode) {
_mode = mode % (_factories.length - 1);// possibly 0 | 1 in this case, but inject as many factories as you wish
},
getFirstName: function() {
return _factories[_mode].firstName;
},
getLastName: function() {
return _factories[_mode].lastName;
}
}
});
最后将它注入你的控制器,并调用它的方法
app.controller('MyController', function($scope, FactoryService) {
// select the first factory (employerFactory)
FactoryService.setMode(0);
$scope.firstName = FactoryService.getFirstName();
$scope.lastName = FactoryService.getLastName();
console.log( $scope.firstName, $scope.lastName);
// select the second factory (employeeFactory)
FactoryService.setMode(1);
$scope.firstName = FactoryService.getFirstName();
$scope.lastName = FactoryService.getLastName();
console.log( $scope.firstName, $scope.lastName);
});
切换逻辑可以基于任何你想要的,这只是它如何工作的一个例子。
假设我有多个工厂,这些工厂内部有相同的函数和变量。例如,
var app = angular.module('app', []);
app.factory('employerFactory', function () {
return {
firstName: 'Kristoffer',
lastName: 'Karlsson',
};
});
app.factory('employeeFactory', function () {
return {
firstName: 'Alice',
lastName: 'Guo',
};
});
现在我有一个这样的控制器:
app.controller('MyController', function($scope, myFactory) {
$scope.firstName = myFactory.firstName;
$scope.lastName = myFactory.lastName;
});
我的问题是如何启动两个控制器,一个作为依赖传入employerFactory,另一个是employeeFactory。我知道 routerProvider 可以帮助为不同的 url 路由不同的控制器。我可以操纵它以将不同的工厂传递给控制器吗?像
$routeProvider.
when('/a', {
templateUrl: 'test.html',
controller: 'MyController' // should get passed factory 'employerFactory'
}).
when('/b', {
templateUrl: 'test.html',
controller: 'MyController' // should get passed factory 'employeeFactory'
});
只需使用一个服务作为一个额外的层来处理切换逻辑,将两个工厂注入其中,然后创建方法来处理通用功能:
app.service('FactoryService', function($scope, employerFactory, employeeFactory) {
var _mode = 0;
var _factories = [employerFactory, employeeFactory];
return {
setMode: function(mode) {
_mode = mode % (_factories.length - 1);// possibly 0 | 1 in this case, but inject as many factories as you wish
},
getFirstName: function() {
return _factories[_mode].firstName;
},
getLastName: function() {
return _factories[_mode].lastName;
}
}
});
最后将它注入你的控制器,并调用它的方法
app.controller('MyController', function($scope, FactoryService) {
// select the first factory (employerFactory)
FactoryService.setMode(0);
$scope.firstName = FactoryService.getFirstName();
$scope.lastName = FactoryService.getLastName();
console.log( $scope.firstName, $scope.lastName);
// select the second factory (employeeFactory)
FactoryService.setMode(1);
$scope.firstName = FactoryService.getFirstName();
$scope.lastName = FactoryService.getLastName();
console.log( $scope.firstName, $scope.lastName);
});
切换逻辑可以基于任何你想要的,这只是它如何工作的一个例子。