首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角定义/加载/配置模块

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

Stack Overflow用户
提问于 2016-01-25 02:21:09
回答 1查看 21关注 0票数 0

下面两种编写代码的方法在功能方面有什么不同?

我坚持这个代码编写风格

我尝试在我的代码中,但第二种形式打破了代码,我没有张贴整个代码集中在主要部分。谢谢

代码语言:javascript
复制
var myApp = angular.module('MainMenuCtrl', ['ngAnimate']);
myApp.controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-25 03:09:54

第二种方法更模块化,因为您可以获取其中的一部分,并立即将其放到另一个项目中,而不必查看app变量(碰巧是全局变量)是否与您插入的项目匹配。

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

此外,构建和搭建工具不需要设置任何变量

代码语言:javascript
复制
// 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]);

)}();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34984177

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档