举个例子:
(function() {
angular.module('Base', []).controller('BaseController', function($scope) {
$scope.mixin1 = function() {};
})
})();
封装angularjs
模块有什么意义呢?我以为默认是这样。
最佳答案
为避免污染全局命名空间,在编译/连接期间将所有函数包装在一个 IIFE 中,这将产生如下内容:如果您不封装您的函数,那么您将污染全局范围,这并不好。 read this
(function () {
angular.module('app', []);
// MainCtrl.js
function MainCtrl () {
}
angular
.module('app')
.controller('MainCtrl', MainCtrl);
// AnotherCtrl.js
function AnotherCtrl () {
}
angular
.module('app')
.controller('AnotherCtrl', AnotherCtrl);
// and so on...
})();
关于javascript - 将模块隐藏在闭包中的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26601819/