注入依赖到控制器 - angularjs [英] Injecting dependencies into controllers - angularjs

查看:148
本文介绍了注入依赖到控制器 - angularjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是$模式服务来调用控制器及其相应的模板。它看起来是这样的:

I am using the $modal service to invoke a controller and its respective template. it looks something like this :

$modal.open({
  templateUrl: 'someTemplate.tpl.html'
  controller: 'MyController',
  resolve: {
    variable1: function() {return data1;},
    variable2: function() {return data2;}
  }    
});

问题的关键是,当被myController的引用变量1和变量2,解析并传递到该控制器

The point is when MyController is invoked variable1 and variable2 are resolved and passed into this controller

我现在需要从其他地方的code调用这个控制器(未通过$模式服务)。如何我注入变量1和变量2,在这种情况下?

I now need to call this controller from elsewhere in the code (not via $modal service). How to I inject variable1 and variable2 in this case ?

推荐答案

您可以使用 $控制器

You can use $controller:

app.run(['$controller', function($controller){

   $controller('MyController', { 
      variable1: 'something' , 
      variable2: 'another'
   });

}]);

这篇关于注入依赖到控制器 - angularjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆