在AngularJS中声明控制器 [英] Declaring controllers in AngularJS
问题描述
函数FirstController($ scrope){
//用$范围
做些事情}
和其他这样做:
var FirstController = function($ scope){
//使用范围
在JS文件中声明控制器的最好方法是哪种方法使用最新版本的AngularJS(现在1.0.7)最好,最佳做法是什么?或者它并不重要?
您应该遵循他们提供的第二个示例,它使用字符串来标识控制器而不是潜在的全球功能。使用Array语法,您可以缩小代码而不用担心重命名函数参数。
'对myApp');myApp.controller('GreetingCtrl',['$ scope',function($ scope){
$ scope.greeting ='Hola!';
}]);
来源: http://docs.angularjs.org/guide/controller
I've seen that on AngularJS tutorials, that some people declare their controller functions like so:
function FirstController($scrope) {
// do something with $scope
}
and other have done it like this:
var FirstController = function ($scope) {
// do something with scope
}
Which way is the best way to declare a controller in your JS file, that will work best with the latest version of AngularJS ( right now 1.0.7 ), as in what are the best practices? Or does it not really matter?
You should follow the second example they offer, which uses a string to identify your controller rather than a potentially global function. Use the Array syntax so you can minify your code without worrying about the minifier renaming function parameters.
var myApp = angular.module('myApp');
myApp.controller('GreetingCtrl', ['$scope', function($scope) {
$scope.greeting = 'Hola!';
}]);
Source: http://docs.angularjs.org/guide/controller
这篇关于在AngularJS中声明控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!