角度定义/加载/配置模块

时间:2016-01-25 02:21:09

标签: angularjs

在功能方面,编写下面代码的两种方法有什么区别?

我坚持这个code writing style

我尝试了我的代码,但第二种形式打破了代码,我没有发布整个代码,专注于主要部分。感谢

var myApp = angular.module('MainMenuCtrl', ['ngAnimate']);
myApp.controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);

 angular
    .module('MainMenuCtrl', ['ngAnimate'])
    .controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);

1 个答案:

答案 0 :(得分:1)

第二种方法更模块化,因为你可以抓住它的一部分并立即将它放入另一个项目中,而不必看看恰好是全局的app变量是否与你插入它的项目相匹配成。

此外,您可以将所有组件包装在IIFE中并包含"使用严格的"而不是强制它在页面中的任何其他脚本。

另外,构建和脚手架工具不需要设置任何变量

// in one file
;(function(){

    "use strict";
     // var app wouldn't be available in the next file if it was used here

     angular
        .module('MainMenuCtrl', ['ngAnimate'])
        .controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);
)}();

// in another file
;(function(){    
    "use strict";

     angular
        .module('MainMenuCtrl')
        .controller('AnotherCtrl', ['$scope', '$http',AnotherCtrl]);

)}();
相关问题