在控制器之间共享AngularJS工厂
我有两个控制器和一个工厂,两个控制器中都使用了一些实用功能。其中一个实用程序函数检查缓存(在控制器之间共享AngularJS工厂,angularjs,Angularjs,我有两个控制器和一个工厂,两个控制器中都使用了一些实用功能。其中一个实用程序函数检查缓存($cacheFactory)中的某个值,无论发生什么情况,我都无法让它工作 应用程序: angular.module('sfmiet', ['sfmiet.autocomplete', 'sfmiet.httpservice', 'sfmiet.utilservice']); angular.module('sfmiet.utilservice', []) .factory('utilservic
$cacheFactory
)中的某个值,无论发生什么情况,我都无法让它工作
应用程序:
angular.module('sfmiet', ['sfmiet.autocomplete', 'sfmiet.httpservice', 'sfmiet.utilservice']);
angular.module('sfmiet.utilservice', [])
.factory('utilservice', ['$cacheFactory', function($cacheFactory) {
return function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
this.init = function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
};
this.isActiveStep = function(step) {
return (this.cache.get('step') == step);
};
};
}]);
angular.module('sfmiet').controller('sftestCtrl', function($scope, $cacheFactory, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
angular.module('sfmiet').controller('sfmietCtrl', function($scope, $timeout, $http, $cacheFactory, autocomplete, httpservice, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
工厂:
angular.module('sfmiet', ['sfmiet.autocomplete', 'sfmiet.httpservice', 'sfmiet.utilservice']);
angular.module('sfmiet.utilservice', [])
.factory('utilservice', ['$cacheFactory', function($cacheFactory) {
return function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
this.init = function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
};
this.isActiveStep = function(step) {
return (this.cache.get('step') == step);
};
};
}]);
angular.module('sfmiet').controller('sftestCtrl', function($scope, $cacheFactory, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
angular.module('sfmiet').controller('sfmietCtrl', function($scope, $timeout, $http, $cacheFactory, autocomplete, httpservice, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
控制器1:
angular.module('sfmiet', ['sfmiet.autocomplete', 'sfmiet.httpservice', 'sfmiet.utilservice']);
angular.module('sfmiet.utilservice', [])
.factory('utilservice', ['$cacheFactory', function($cacheFactory) {
return function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
this.init = function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
};
this.isActiveStep = function(step) {
return (this.cache.get('step') == step);
};
};
}]);
angular.module('sfmiet').controller('sftestCtrl', function($scope, $cacheFactory, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
angular.module('sfmiet').controller('sfmietCtrl', function($scope, $timeout, $http, $cacheFactory, autocomplete, httpservice, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
控制器2:
angular.module('sfmiet', ['sfmiet.autocomplete', 'sfmiet.httpservice', 'sfmiet.utilservice']);
angular.module('sfmiet.utilservice', [])
.factory('utilservice', ['$cacheFactory', function($cacheFactory) {
return function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
this.init = function(cacheName) {
this.cache = $cacheFactory.get(cacheName);
};
this.isActiveStep = function(step) {
return (this.cache.get('step') == step);
};
};
}]);
angular.module('sfmiet').controller('sftestCtrl', function($scope, $cacheFactory, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
angular.module('sfmiet').controller('sfmietCtrl', function($scope, $timeout, $http, $cacheFactory, autocomplete, httpservice, utilservice) {
$scope.util = new utilservice('stateCache');
console.log($scope.util);
//some more stuff...
});
控制器的日志$scope.util.cache
为第二个控制器提供了一个对象,但第一个控制器未定义。不使用new关键字会使第一个控制器的$scope.util
完全未定义,而第二个控制器的情况也很好。只有一个控制器,一切都可以正常工作,但一旦我引入第二个控制器,它就会停止工作
也许我遗漏了什么,但我看不出这种行为的原因。我在网上搜索了3个小时,现在我已经筋疲力尽了,希望你们能给我一个机会。非常感谢 根据,$cacheFactory.get
如果缓存不存在,则返回未定义的
最可能的原因是
stateCache
是在第一个控制器之后、第二个控制器之前创建的 你对此有什么建议吗?你是对的,我完全没有想到。非常感谢你!顺便说一句,有没有比在控制器中更好的地方来创建它,这样它就不依赖于它们的顺序了?在这种情况下,我会在utilService中创建它。由于utilService上两个控制器的依赖关系,保证首先实例化。在这种情况下,您可能希望使用.service
而不是.factory
,因此所有新的内容都将为您处理,我将对此进行研究。非常感谢你的帮助。