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

查看:127
本文介绍了在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屋!

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