markdown [控制器] #angular

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown [控制器] #angular相关的知识,希望对你有一定的参考价值。

### Controllers

Use a controller to augment shared `scope`.
* To decorate it with `value` that HTML `partials` may bind to.
* To provide `behaviour` that HTML `partials` may invoke.

Do **not** use a controller:
* To store application state (describe in the `route` instead).
* To store persistent data (controllers and scope are dropped on every route change, use `service` instead).
* To implement behaviour and business logic (defer to a `service`).

In angular, controllers are implemented as a `Class`. Angular will resolve the dependency as a class and then `new` that class. Therefore the mapping should be `PascalCase` as well as the classname itself. For example:
```
angular.module(...).controller('MyController', MyController);
```

In our implementation you can effectively assume that `scope instanceof controller`. This is because in our `constructor` we will `extend()` the scope with the members of the instance.

>Extend is a method that will copy members of one object to another. It was popularised by [jQuery.extend()](http://api.jquery.com/jquery.extend/) but we require a different implementation that will copy accessors (get/set) correctly.

We will therefore need to `bind()` these members (accessors and functions) to ensure that their `this` reference is not lost.

>In javascript instance methods can easily loose their `this` reference. We can leverage [Function.bind()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) method can to bind all functions and accessors of a given object.

The controller is somewhat similar to the directive `link()` method. An external link implementation should look exactly like a controller but may inject `element` and `attributes`.

Behaviour should follow the [flyweight](http://en.wikipedia.org/wiki/Flyweight_pattern) pattern. Meaning that functions should gather operands from the `scope` and delegate to a method on a `service`. It should **not** implement the operation directly.

#### constructor

Injection is available in the constructor only. This must be annotated with the `@ngInject` doctag.

Firstly `bind` the instance and `extend` scope.

Initialise any `private` properties.

Add `watch` statements. Handlers may be organised as members of the class, or may be anonymous.

#### functions, accessors, properties

Public `properties` (i.e. variables) are not permitted. Use accessors, meaning `getter` and/or `setter` methods.

The `function` keyword is **not** needed.

The `'use strict'` statement is **not** needed.

#### private properties

Private properties should have the same name as any public property that it shadows. A **trailing underscore** is required per [google javascript style](https://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Naming#Naming).

以上是关于markdown [控制器] #angular的主要内容,如果未能解决你的问题,请参考以下文章

markdown 快捷方式Angular

markdown Angular:ReactiveForm - 使用数组

markdown JHipster - 在Angular上导入模块

markdown Angular_Firebase_basic

markdown Angular2 Snippets - 路由

markdown Angular2 Snippets - Observables