首页 > 代码库 > Angularjs[补21] - 显示注入,隐示注入

Angularjs[补21] - 显示注入,隐示注入

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div ng-app="myApp">
        <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>
</body>
</html>
var myApp = angular.module(‘myApp‘,[],function () {

})

//隐示依赖注入
.controller(‘firstController‘,function ($scope,CustomService) {

})
//显示依赖注入(推荐)
.controller(‘secondController‘,[‘$scope‘,‘$filter‘,function (a,b) {
    console.log(a);
    console.log(b(‘json‘)([1,2,3,4,5]));
}]);

function otherController(a) {
    console.log(a);
}
otherController.$inject = [‘$scope‘];

技术分享

  • 隐示注入,在 js 进行压缩时候会出错,因为变量名变短,而依赖注入是基于其服务的名称。
  • 显示注入代码压缩不出错。

 

Angularjs[补21] - 显示注入,隐示注入