原文地址:https://docs.angularjs.org/guide/controller

Understanding Controllers

理解控制器

In AngularJS,aController is defined by a JavaScriptconstructor functionthatis used to augment theAngularJS Scope.

AngularJS中,一个控制器被一个JavaScript构造函数定义,用来扩大AngularJS Scope

When aController is attached to the DOM via theng-controllerdirective,AngularJS willinstantiate a new Controller object,using the specified Controller'sconstructorfunction. A newchild scopewill be created and madeavailable as an injectable parameter to the Controller's constructor functionas$scope.

当一个控制器被通过ng-controller指示符附在一个DOM上,AngularJS将会实例化一个控制器对象--使用控制器的构造函数。一个新的child scope将会被创建,可以以$scope的名字,作为一个对于控制器构造方法可被注入的参数开始生效。

If thecontroller has been attached using thecontrollerasSyntaxthen the controller instance will be assigned to a property on the new scope.

如果控制器被使用controller as语法而附加上,控制器实例将会被分配给新的scope的一个属性。

Use controllersto:

  • Set up the initial state of the$scopeobject.
  • Add behavior to the$scopeobject.

使用控制器到:

  • 设置$scope对象的初始状态。
  • 给$scope对象增加行为。

Do not use controllers to:

不要使用控制器到:

  • Manipulate DOM — Controllers should contain only business logic. Putting any presentation logic into Controllers significantly affects its testability. AngularJS hasdatabindingfor most cases anddirectivesto encapsulate manual DOM manipulation.
  • 操控DOM----控制器应该只包含业务逻辑。把任何展示逻辑放入控制器会影响它的可测试性。AngularJS对于大多数情况有databinding和directives去封装手工的DOM操作。
  • Format input — UseAngularJS form controlsinstead.
  • 表单输入----使用AngularJS form 控制替代。
  • Filter output — UseAngularJS filtersinstead.
  • 过滤输出----使用AngularJS filters替代。
  • Share code or state across controllers — UseAngularJS servicesinstead.
  • 在控制器间共享代码和状态----使用AngularJS services替代。
  • Manage the life-cycle of other components (for example,to create service instances).
  • 管理其它组件的声明周期(例如,去创建service实例)。

Setting up theinitial state of a$scopeobject

建立$scope对象的初始状态

Typically,whenyou create an application you need to set up the initial state for theAngularJS$scope. You set up the initial state of a scopeby attaching properties to the$scopeobject.The properties contain theview model(the model that will bepresented by the view). All the$scopepropertieswill be available to thetemplateat the point in the DOM wherethe Controller is registered.

典型的,当你创建一个应用,你需要去建立AngularJS$scope的初始状态。你通过给$scope对象附着上属性设置一个scope的初始状态。这些属性包含这个view model(被那个view所呈现的model)。所有这些$scope属性对于template是可用的----Controller被注册的DOM的那个点上。

The followingexample demonstrates creating aGreetingController,which attachesagreetingproperty containing the string'Hola!'to the$scope:

以下例子展示了创建一个GreetingController,这附着了一个greeting属性(包含着字符串“Hola”)给$cope

var myApp =angular.module('myApp',[]);

myApp.controller('GreetingController',['$scope',function($scope) {

$scope.greeting = 'Hola!';

}]);

We createanAngularJS Module,myApp,for our application. Then we add the controller's constructor functionto the module using the.controller()method.This keeps the controller's constructor function out of the global scope.

我们创建一个AngularJS ModulemyApp,给我们的应用。然后我们增加这个控制器的构造函数给模块----使用.controller()方法。这使得控制器的构造函数在全局范围之外。

We have used aninline injection annotationtoexplicitly specify the dependency of the Controller on the$scopeservice provided by AngularJS. See the guide onDependency Injectionfor moreinformation.

我们使用了一个inline injectionannotation去显式地指定被AngularJS提供的$scope service之上的控制器的依赖。参看Dependency Injection的指导来获取更多信息。

We attach ourcontroller to the DOM using theng-controllerdirective.

我们附着我们的控制器到DOM----使用ng-controlle指示符。

Thegreetingproperty can Now be data-bound to the template:

这个greeting属性现在可以被数据限制(data-bound)在模板中。

<divng-controller="GreetingController">

{{ greeting }}

</div>

Adding Behaviorto a Scope Object

Scope对象增加行为

In order toreact to events or execute computation in the view we must provide behavior tothe scope. We add behavior to the scope by attaching methods to the$scopeobject. These methods are then available to be called from thetemplate/view.

为了在view中对events或者execute computation做出反应,我们必须提供行为给scope。我们增加行为给scope----通过附着方法给$scope对象。这些方法然后在这个template/view中被调用是有效的。

The followingexample uses a Controller to add a method,which doubles a number,to thescope:

下面的例子使用一个Controller去增加方法--这个方法对一个数字进行翻倍scope

var myApp =angular.module('myApp',[]);

myApp.controller('DoubleController',function($scope){

$scope.double = function(value) { returnvalue * 2; };

}]);

Once theController has been attached to the DOM,thedoublemethod canbe invoked in an AngularJS expression in the template:

一旦这个控制器被附着给DOM,这个double方法就可以被调用在一个AngularJS表达式中,在这个模板里面。

<divng-controller="DoubleController">

Two times <inputng-model="num"> equals {{ double(num) }}

</div>

As discussed intheConceptssection of this guide,anyobjects (or primitives) assigned to the scope become model properties. Anymethods assigned to the scope are available in the template/view,and can beinvoked via AngularJS expressions andngeventhandler directives (e.g.ngClick).

就像这个指南的Concepts章节讨论的,任何对象(或者原语)分配给这个scope就成为了model的属性。任何方法分配给scope就在template/view中有效,并且可以被调用,通过AngularJS的表达式和ng event handler 指示符(例如:ngClick)。

Using ControllersCorrectly

In general,aController shouldn't try to do too much. It should contain only the businesslogic needed for a single view.

The most commonway to keep Controllers slim is by encapsulating work that doesn't belong tocontrollers into services and then using these services in Controllers viadependency injection. This is discussed in theDependency InjectionandServicessections of this guide.

Associating Controllers withAngularJS Scope Objects

You canassociate Controllers with scope objects implicitly via thengController directiveor$route service.

Simple SpicyController Example

To illustratefurther how Controller components work in AngularJS,let's create a little appwith the following components:

  • Atemplatewith two buttons and a simple message
  • A model consisting of a string namedspice
  • A Controller with two functions that set the value ofspice

The message in our template contains a binding to thespicemodel which,by default,is set to the string "very".Depending on which button is clicked,thespicemodel isset tochiliorjalapeño,and themessage is automatically updated by data-binding.

Edit in Plunker

index.htmlapp.js

<divng-controller="SpicyController">

<buttonng-click="chiliSpicy()">Chili</button>

<buttonng-click="jalapenoSpicy()">Jalapeño</button>

<p>The food is{{spice}} spicy!</p>

</div>

Things to noticein the example above:

  • Theng-controllerdirective is used to (implicitly) create a scope for our template,and the scope is augmented (managed) by theSpicyControllerController.
  • SpicyControlleris just a plain JavaScript function. As an (optional) naming convention the name starts with capital letter and ends with "Controller".
  • Assigning a property to$scopecreates or updates the model.
  • Controller methods can be created through direct assignment to scope (see thechiliSpicymethod)
  • The Controller methods and properties are available in the template (for both the<div>element and its children).

Spicy ArgumentsExample

Controllermethods can also take arguments,as demonstrated in the following variation ofthe prevIoUs example.

Edit in Plunker

index.htmlapp.js

<divng-controller="SpicyController">

<inputng-model="customSpice">

<buttonng-click="spicy('chili')">Chili</button>

<buttonng-click="spicy(customSpice)">Custom spice</button>

<p>The food is{{spice}} spicy!</p>

</div>

Notice thattheSpicyControllerControllerNow defines just one method calledspicy,which takesone argument calledspice. The templatethen refers to this Controller method and passes in a string constant'chili'in the binding for the first button and a modelpropertycustomSpice(bound toan input Box) in the second button.

ScopeInheritance Example

It is common toattach Controllers at different levels of the DOM hierarchy. Since theng-controllerdirective creates a newchild scope,we get a hierarchy of scopes that inherit from each other.The$scopethat each Controller receives willhave access to properties and methods defined by Controllers higher up thehierarchy. SeeUnderstanding Scopesformore information about scope inheritance.

Edit in Plunker

index.htmlapp.cssapp.js

<divclass="spicy">

<divng-controller="MainController">

<p>Good {{timeOfDay}},{{name}}!</p>

<divng-controller="ChildController">

<p>Good {{timeOfDay}},{{name}}!</p>

<divng-controller="GrandChildController">

<p>Good{{timeOfDay}},{{name}}!</p>

</div>

</div>

</div>

</div>

Notice how wenested threeng-controllerdirectivesin our template. This will result in four scopes being created for our view:

  • The root scope
  • TheMainControllerscope,which containstimeOfDayandnameproperties
  • TheChildControllerscope,which inherits thetimeOfDayproperty but overrides (shadows) thenameproperty from the prevIoUs scope
  • TheGrandChildControllerscope,which overrides (shadows) both thetimeOfDayproperty defined inMainControllerand thenameproperty defined inChildController

Inheritance works with methods in the same way as it does with properties.so in our prevIoUs examples,all of the properties Could be replaced withmethods that return string values.

Testing Controllers

Although thereare many ways to test a Controller,one of the best conventions,shown below,involves injecting the$rootScopeand$controller:

ControllerDeFinition:

var myApp =angular.module('myApp',[]);

myApp.controller('MyController',function($scope) {

$scope.spices = [{"name":"pasilla","spiciness":"mild"},

{"name":"jalapeno","spiciness":"hot hothot!"},

{"name":"habanero","spiciness":"LAVAHOT!!"}];

$scope.spice = "habanero";

});

Controller Test:

describe('myControllerfunction',function() {

describe('myController',function() {

var $scope;

beforeEach(module('myApp'));

beforeEach(inject(function($rootScope,$controller) {

$scope = $rootScope.$new();

$controller('MyController',{$scope:$scope});

}));

it('should create"spices" model with 3 spices',function() {

expect($scope.spices.length).toBe(3);

});

it('should set thedefault value of spice',function() {

expect($scope.spice).toBe('habanero');

});

});

});

If you need totest a nested Controller you must create the same scope hierarchy in your testthat exists in the DOM:

describe('state',function() {

var mainScope,childScope,grandChildScope;

beforeEach(module('myApp'));

beforeEach(inject(function($rootScope,$controller) {

mainScope = $rootScope.$new();

$controller('MainController',{$scope:mainScope});

childScope = mainScope.$new();

$controller('ChildController',{$scope:childScope});

grandChildScope = childScope.$new();

$controller('GrandChildController',{$scope:grandChildScope});

}));

it('should have overand selected',function() {

expect(mainScope.timeOfDay).toBe('morning');

expect(mainScope.name).toBe('Nikki');

expect(childScope.timeOfDay).toBe('morning');

expect(childScope.name).toBe('Mattie');

expect(grandChildScope.timeOfDay).toBe('evening');

expect(grandChildScope.name).toBe('GingerbreadBaby');

});

});

Angular学习:控制器未翻译完的更多相关文章

  1. Android Studio是否支持用于Android UI设计的AngularJS?

    我对AndroidStudio有疑问:AS在设计XML文件时是否支持AngularJS代码,例如:对于小动画或效果?

  2. android – 如何使用ClientID和ClientSecret在Phonegap中使用Angularjs登录Google OAuth2

    我正尝试使用Angularjs(使用IonicFramework)通过GoogleOAuth2从我的Phonegap应用程序登录.目前我正在使用http://phonegap-tips.com/articles/google-api-oauth-with-phonegaps-inappbrowser.html进行登录.但是当我使用Angular-UI-RouterforIonic时,它正在创建非常

  3. 利用require.js与angular搭建spa应用的方法实例

    这篇文章主要给大家介绍了关于利用require.js与angular搭建spa应用的方法实例,文中通过示例代码给大家介绍的非常详细,对大家的理解和学习具有一定的参考学习价值,需要的朋友们下面跟着小编来一起看看吧。

  4. 详解Angular动态组件

    本文主要介绍了Angular动态组件,对此感兴趣的同学,可以亲自实验一下。

  5. 详解如何使用webpack+es6开发angular1.x

    本篇文章主要介绍了详解如何使用webpack+es6开发angular1.x,具有一定的参考价值,有兴趣的可以了解一下

  6. angular2系列之路由转场动画的示例代码

    本篇文章主要介绍了angular2系列之路由转场动画的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  7. 一种angular的方法级的缓存注解(装饰器)

    本篇文章主要介绍了一种angular的方法级的缓存注解(装饰器),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  8. 动手写一个angular版本的Message组件的方法

    本篇文章主要介绍了动手写一个angular版本的Message组件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  9. angular forEach方法遍历源码解读

    这篇文章主要为大家详细了angular forEach方法遍历源码,forEach()方法用于遍历对象或数组,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Angular的MVC和作用域

    本文主要Angular的MVC和作用域进行详细分析介绍,具有一定的参考价值,下面跟着小编一起来看下吧

随机推荐

  1. Angular2 innerHtml删除样式

    我正在使用innerHtml并在我的cms中设置html,响应似乎没问题,如果我这样打印:{{poi.content}}它给了我正确的内容:``但是当我使用[innerHtml]=“poi.content”时,它会给我这个html:当我使用[innerHtml]时,有谁知道为什么它会剥离我的样式Angular2清理动态添加的HTML,样式,……

  2. 为Angular根组件/模块指定@Input()参数

    我有3个根组件,由根AppModule引导.你如何为其中一个组件指定@input()参数?也不由AppModalComponent获取:它是未定义的.据我所知,你不能将@input()传递给bootstraped组件.但您可以使用其他方法来做到这一点–将值作为属性传递.index.html:app.component.ts:

  3. angular-ui-bootstrap – 如何为angular ui-bootstrap tabs指令指定href参数

    我正在使用角度ui-bootstrap库,但我不知道如何为每个选项卡指定自定义href.在角度ui-bootstrap文档中,指定了一个可选参数select(),但我不知道如何使用它来自定义每个选项卡的链接另一种重新定义问题的方法是如何使用带有角度ui-bootstrap选项卡的路由我希望现在还不算太晚,但我今天遇到了同样的问题.你可以通过以下方式实现:1)在控制器中定义选项卡href:2)声明一个函数来改变控制器中的散列:3)使用以下标记:我不确定这是否是最好的方法,我很乐意听取别人的意见.

  4. 离子框架 – 标签内部的ng-click不起作用

    >为什么标签标签内的按钮不起作用?>但是标签外的按钮(登陆)工作正常,为什么?>请帮我解决这个问题.我需要在点击时做出回复按钮workingdemo解决方案就是不要为物品使用标签.而只是使用divHTML

  5. Angular 2:将值传递给路由数据解析

    我正在尝试编写一个DataResolver服务,允许Angular2路由器在初始化组件之前预加载数据.解析器需要调用不同的API端点来获取适合于正在加载的路由的数据.我正在构建一个通用解析器,而不是为我的许多组件中的每个组件设置一个解析器.因此,我想在路由定义中传递指向正确端点的自定义输入.例如,考虑以下路线:app.routes.ts在第一个实例中,解析器需要调用/path/to/resourc

  6. angularjs – 解释ngModel管道,解析器,格式化程序,viewChangeListeners和$watchers的顺序

    换句话说:如果在模型更新之前触发了“ng-change”,我可以理解,但是我很难理解在更新模型之后以及在完成填充更改之前触发函数绑定属性.如果您读到这里:祝贺并感谢您的耐心等待!

  7. 角度5模板形式检测形式有效性状态的变化

    为了拥有一个可以监听其包含的表单的有效性状态的变化的组件并执行某些组件的方法,是reactiveforms的方法吗?

  8. Angular 2 CSV文件下载

    我在springboot应用程序中有我的后端,从那里我返回一个.csv文件WheniamhittingtheURLinbrowsercsvfileisgettingdownloaded.现在我试图从我的角度2应用程序中点击此URL,代码是这样的:零件:服务:我正在下载文件,但它像ActuallyitshouldbeBook.csv请指导我缺少的东西.有一种解决方法,但您需要创建一个页面上的元

  9. angularjs – Angular UI-Grid:过滤后如何获取总项数

    提前致谢:)你应该避免使用jQuery并与API进行交互.首先需要在网格创建事件中保存对API的引用.您应该已经知道总行数.您可以使用以下命令获取可见/已过滤行数:要么您可以使用以下命令获取所选行的数量:

  10. angularjs – 迁移gulp进程以包含typescript

    或者我应该使用tsc作为我的主要构建工具,让它解决依赖关系,创建映射文件并制作捆绑包?

返回
顶部