考虑以下示例。他们的工作方式不同吗?
.controller('MyController',function($scope) {
$scope.username = 'World';
$scope.sayHello = function() {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
});
对比
.controller('MyController', ['$scope', function($scope) {
$scope.username = 'World';
$scope.sayHello = function() {
$scope.greeting = 'Hello ' + $scope.username + '!';
};
}]);
他们为我输出相同的内容,我只是不明白为什么我们把它放在那里。有时我不得不把它作为对象来工作。为什么要定义两次?
最佳答案
这是个有趣的问题。答案是两者都在本地环境中按预期工作。但是..
Array notation is useful when you Minify/obfuscate JavaScript files for production release. In that case the argument $scope might be renamed to $s, but because we defined string ‘$scope’ as first argument, Angular is aware that first dependency to this controller is $scope object.
因此,除非您正在编写用于内部目的的东西,否则请使用推荐的数组表示法! :)
引用这个:http://viralpatel.net/blogs/angularjs-controller-tutorial/
关于javascript - Angular 两次定义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006202/