下面两种编写代码的方法在功能方面有什么不同?
我坚持这个代码编写风格。
我尝试在我的代码中,但第二种形式打破了代码,我没有张贴整个代码集中在主要部分。谢谢
var myApp = angular.module('MainMenuCtrl', ['ngAnimate']);
myApp.controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);
angular
.module('MainMenuCtrl', ['ngAnimate'])
.controller('MainMenuCtrl', ['$scope', '$http', MainMenu]);发布于 2016-01-25 03:09:54
第二种方法更模块化,因为您可以获取其中的一部分,并立即将其放到另一个项目中,而不必查看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]);
)}();https://stackoverflow.com/questions/34984177
复制相似问题