本文介绍了AngularJS动态范围注入或控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在运行过程中注入范围或控制器?
或任何其他建议动态中注入服务控制器?
Is it possible to inject scope or controller during running ?or any other advice to dynamically inject services into controller ?
Application.controller('IndexController', function($scope){
// some actions
if(someconditions) {
$scope.$inject = [someServiceName];
// and here i want to use service methods
}
});
在此先感谢
推荐答案
一个服务可以使用的。如果能够通过控制器参数注入的服务就是这样角度提供了方便。引擎盖下,在$喷油器所使用的角度来检索对象实例。但是,我们可以使用$喷射器还自己
A service can be dynamically injected (by name) into a controller using the $injector. Being able to inject services via controller arguments is just a convenience that Angular provides. Under the hood, the $injector is used by Angular to retrieve object instances. But we can use the $injector ourselves also.
function MyCtrl($scope, $injector) {
$scope.doSomething = function(someService) {
var service = $injector.get(someService) // someService contains the name of a service
service.value += 10
}
。
这篇关于AngularJS动态范围注入或控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!