Inside the controller I am trying to breakup my code into named functions for readability. However, in the parameterized named functions the scope and the injected dependency are all null. How do access these inside the named functions. Thanks for you help.
在控制器内部,我试图将我的代码分解为命名函数以提高可读性。但是,在参数化的命名函数中,作用域和注入的依赖项都是null。如何在命名函数中访问这些函数。谢谢你的帮助。
(
function() {
'use strict';
var moduleName = 'ufsrAppModule';
var controllerName = 'ufsrController';
var dependencyInjection = ['api', 'appHost', 'userAccount', 'userProfileFactory', 'fsrFactory', 'userFsrFactory', internalFunc];
angular.module(moduleName)
.controller(controllerName, dependencyInjection);
function internalFunc(api, appHost, userAccount, userProfileFactory, fsrFactory, userFsrFactory) {
var vm = this; //controller AS in ng-controller, do not use $scope
init(api, appHost, userAccount, userProfileFactory, fsrFactory, userFsrFactory, vm);
}
function init(api, appHost, userAccount, userProfileFactory, fsrFactory, userFsrFactory, vm) {
vm.facilityChanged = facilityChanged;
...
...
function facilityChanged(vm, fsrFactory) {
/*update UI then retrieve services*/
vm.postStatus = undefined;
vm.services = undefined;
vm.roles = undefined;
vm.services = fsrFactory.service().query({
/*parameters*/
FacilityID: vm.facility
})
.$promise.then(
function(data) {
vm.services = data;
});
}
}
})();
2 个解决方案
#1
0
Strict DI can be done separately in this style
严格的DI可以这种方式单独完成
angular.module(moduleName)
.controller(controllerName, controllerFunction);
controllerFunction.$inject = ['$scope', '$http'];
function controllerFunction($scope, $http) {
...
}
This style is also recommended by John Papa's Angular style guide.
John Papa的Angular风格指南也推荐这种风格。
#2
0
The facilityChanged
is not working because its parameters are overwriting those that are passed into init
facilityChanged无法正常工作,因为它的参数会覆盖传递给init的参数
It can be fixed by changing
它可以通过改变来修复
function facilityChanged(vm, fsrFactory) {
to
function facilityChanged() {
Edit: Attached jsbin
编辑:附上jsbin
I strongly recommend putting the init
function inside your controller function to save the parameter passing, just like the activate
function in John Papa's guide.
我强烈建议将init函数放在控制器函数中以保存参数传递,就像John Papa指南中的activate函数一样。
Refined jsbin
#1
0
Strict DI can be done separately in this style
严格的DI可以这种方式单独完成
angular.module(moduleName)
.controller(controllerName, controllerFunction);
controllerFunction.$inject = ['$scope', '$http'];
function controllerFunction($scope, $http) {
...
}
This style is also recommended by John Papa's Angular style guide.
John Papa的Angular风格指南也推荐这种风格。
#2
0
The facilityChanged
is not working because its parameters are overwriting those that are passed into init
facilityChanged无法正常工作,因为它的参数会覆盖传递给init的参数
It can be fixed by changing
它可以通过改变来修复
function facilityChanged(vm, fsrFactory) {
to
function facilityChanged() {
Edit: Attached jsbin
编辑:附上jsbin
I strongly recommend putting the init
function inside your controller function to save the parameter passing, just like the activate
function in John Papa's guide.
我强烈建议将init函数放在控制器函数中以保存参数传递,就像John Papa指南中的activate函数一样。
Refined jsbin