angularjs - 运行时依赖注入?

Roy*_*ove 12 angularjs

简单的我希望..

这是一个参考的plunker.

我知道如何在编译时指定依赖项(请参阅MainCtrlInjected控制器).但是,如何在运行时下拉依赖项,给出该依赖项的名称?(参见MainCtrlInjectedRuntime控制器)

mat*_*4pl 16

您可以使用$ injector在运行时获取值:

检查我的分叉的plunker:http://plnkr.co/edit/iVblEU?p = preview

码:

app.controller('MainCtrlInjectedRuntime', [
  '$scope',
  '$injector'
  ($scope, $injector) ->

   nameValHandle = 'nameVal'

   # !!! This is how you inject at runtime
   name = $injector.get(nameValHandle)

   $scope.name = name
])
Run Code Online (Sandbox Code Playgroud)