在 AngularJS 中声明控制器 [英] Declaring controllers in AngularJS

查看:32
本文介绍了在 AngularJS 中声明控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 AngularJS 教程中看到,有些人像这样声明他们的控制器功能:

I've seen that on AngularJS tutorials, that some people declare their controller functions like so:

function FirstController($scrope) {
    // do something with $scope
}

和其他人这样做了:

var FirstController = function ($scope) {
    // do something with scope
}

哪种方式是在 JS 文件中声明控制器的最佳方式,最适合最新版本的 AngularJS(现在是 1.0.7),最佳实践是什么?还是真的无所谓?

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?

推荐答案

您应该遵循他们提供的第二个示例,该示例使用字符串来标识您的控制器而不是潜在的全局函数.使用 Array 语法,这样您就可以缩小代码,而不必担心缩小器重命名函数参数.

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!';
}]);

来源:http://docs.angularjs.org/guide/controller

这篇关于在 AngularJS 中声明控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆