显式和隐式依赖注入
把service当作被依赖的资源加载到controller中的⽅方法,与加载到其他服务中的方法很相似。
javascript是一个动态语言,DI不能弄明白应该通过参数类型注入哪一个service
显式依赖:要通过$inject属性指定service名称, 它是一个包含需要注入的service名称的字符串数组,工厂方法中的参数顺序,与service 在数组中的顺序一致。
隐式依赖:则允许通过参数名称决定依赖,$scope
例子:
index.html
<div ng-app="myApp">
<div ng-controller="firstController">
</div>
<div ng-controller="secondController">
</div>
<div ng-controller="otherController">
</div>
</div>
<script type="text/javascript" src="../../vendor/angular/angularjs.js"></script>
<script type="text/javascript" src="app/index.js"></script>Last updated
Was this helpful?