是否有必要将 $scope 注入 angularjs 中的控制器? [英] Is it necessary to inject $scope to a controller in angularjs?
本文介绍了是否有必要将 $scope 注入 angularjs 中的控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面两个代码片段有什么区别吗?两者都有效.
Is there any difference between the following two code snippet? Both work.
1.
myApp.controller("myAppController", ["$scope", function($scope) {
// function body
}]);
2.
myApp.controller("myAppController", function($scope) {
// function body
});
推荐答案
好吧,在缩小过程中会产生差异.如果您不遵循 step1 ,则缩小会破坏您的代码.
Well, difference will create during minfication. If you don't follow the step1 , minification will break your code.
您的第一个代码的丑化版本
Uglify Version of your 1st code
myApp.controller("myAppController",["$scope",function(o){}])
您的第二个代码的丑化版本
Uglify Version of your 2nd code
myApp.controller("myAppController",function(o){})
如果您按照第 1 步操作,Angular 会从注入中找到 o
的定义.
If you follow step 1 , Angular will find definition of o
from injection.
但是如果您按照第 2 步操作,Angular 将无法从任何来源找到 o
的定义.
But if you follow step 2 , Angular won't find definition of o
from any source.
这篇关于是否有必要将 $scope 注入 angularjs 中的控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文