Original address: Http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model
In the context of the angular document discussion, the term "model" can be applied to a single object representing an entity (for example, a model called "phones" whose value is a telephone array.) ) or as the entire data model (all entities) of the application.
In angular, the model can be arbitrary data and can be obtained through the properties of the angular scope object. The name of the property is the identity of the model, and the value can be any JavaScript object (including arrays and raw data).
The only condition that JavaScript wants to be model is that the object must be referenced by the angular scope as a property of a scope object. The referential relationship of a property can be created either explicitly or implicitly.
There are several ways that you can explicitly create a property of scope and associate a JavaScript object to create a model:
- In JavaScript code, the properties of the scope object are assigned directly, which is usually emitted in the controller:
function Myctrl ($scope) { //create property ' foo ' on the Myctrl ' s scope //and assign it an initial value ' bar '
$scope. foo = ' Bar '; }
- In the template's angular expression (http://www.cnblogs.com/lcllao/archive/2012/09/16/2687162.html), use the assignment operator:
<button ng-click= "{{foos= ' ball '}}" >click me</button>
- Using nginit directive (Http://docs.angularjs.org/api/ng.directive:ngInit) in templates (just as an example, not recommended for real-world applications)
<body ng-init= "foo = ' Bar '" >
Angular creates the model implicitly in the following template structure:
- Input, select, textarea, and other form elements for the form:
<input ng-model= "Query" value= "Fluffy Cloud" >
The above code creates a model called "query" in the current scope, and binds to the value of input, initialized to "fluffy cloud."
- Declaring iterators in Ngrepeater
<p ng-repeat= "Phone in Phones" ></p>
The above code creates a child scope for each element of the phones array, and creates a "phone" model in the corresponding child scope, giving the corresponding value in the array.
In angular, when the following situation occurs, the JavaScript object will no longer be a model:
- When there is no angular scope that contains the properties associated with the object.
- All angular scopes that contain properties associated with an object become stale and appropriate for garbage collection.
The following illustration shows the implicit creation of a simple data model in a simple template.
Angularjs study note--understanding the Model Component