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 phone array.) or as the full data model of the application (all entities).
In angular, the model can be any data, and the model can be obtained through the properties of the scope object of the angular. The name of the property is the identifier of the model, and the value can be any javascript object (including arrays and raw data).
The only condition for JavaScript to become a model is that the object must be referenced as an attribute of a scope object by an angular scope. The reference relationship of an attribute can be created explicitly or implicitly.
We can explicitly create scope properties in the following ways and associate javascript objects to create model:
In javascript code, the attributes assigned to the scope object are directly assigned; this is usually sent 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/lclao/archive/2012/09/16/2687162.html), use the assignment operator:
<button ng-click="{{foos='ball'}}">Click me</button>
Use ngInit directive in templates (http://docs.angularjs.org/api/ng.directive:ngInit) (for example only, it is not recommended in real applications)
<body ng-init=" foo = 'bar' ">
angular will implicitly create a model in the following template structure:
The input, select, textarea and other form elements of the form:
<input ng-model="query" value="fluffy cloud">
The above code creates a model called "query" in the current scope, and is bound to the value of input, and is initialized to "fluffy cloud".
Declare iterator in ngRepeater
<p ng-repeat="phone in phones"></p>
The above code creates a child scope for each element of each phones array, and creates a "phone" model in the corresponding child scope, assigning the corresponding value in the array.
In angular, when the following situation occurs, the javascript object will no longer be a model:
When no angular scope contains the attributes associated with the object.
All angular scopes containing attributes associated with objects become stale and suitable for garbage collection.
The illustration below shows the implicit creation of a simple data model in a simple template.
The above is the information about AngularJS Understanding the Model Component, and we will continue to add it later. Thank you for your support for this site!