angular.js/docs/content/guide/dev_guide.mvc.understanding_model.ngdoc

72 lines
2.9 KiB
Text
Raw Normal View History

2011-06-06 15:50:35 +00:00
@ngdoc overview
@name Developer Guide: About MVC in Angular: Understanding the Model Component
@description
2012-09-26 13:30:55 +00:00
Depending on the context of the discussion in the Angular documentation, the term _model_ can refer to
2011-06-06 15:50:35 +00:00
either a single object representing one entity (for example, a model called "phones" with its value
being an array of phones) or the entire data model for the application (all entities).
2012-09-26 13:30:55 +00:00
In Angular, a model is any data that is reachable as a property of an angular {@link
2012-02-28 20:14:47 +00:00
scope Scope} object. The name of the property is the model identifier and the value is
2011-06-06 15:50:35 +00:00
any JavaScript object (including arrays and primitives).
2012-09-26 13:30:55 +00:00
The only requirement for a JavaScript object to be a model in Angular is that the object must be
referenced by an Angular scope as a property of that scope object. This property reference can be
2011-06-06 15:50:35 +00:00
created explicitly or implicitly.
You can create models by explicitly creating scope properties referencing JavaScript objects in the
following ways:
* Make a direct property assignment to the scope object in JavaScript code; this most commonly
occurs in controllers:
function MyCtrl($scope) {
2011-06-06 15:50:35 +00:00
// create property 'foo' on the MyCtrl's scope
// and assign it an initial value 'bar'
$scope.foo = 'bar';
2011-06-06 15:50:35 +00:00
}
2012-05-24 21:49:47 +00:00
* Use an {@link expression angular expression} with an assignment operator in templates:
2011-06-06 15:50:35 +00:00
<button ng-click="{{foo='bar'}}">Click me</button>
2011-06-06 15:50:35 +00:00
* Use {@link api/ng.directive:ngInit ngInit directive} in templates (for toy/example apps
2011-06-06 15:50:35 +00:00
only, not recommended for real applications):
2012-03-14 02:36:09 +00:00
<body ng-init=" foo = 'bar' ">
2011-06-06 15:50:35 +00:00
Angular creates models implicitly (by creating a scope property and assigning it a suitable value)
when processing the following template constructs:
* Form input, select, textarea and other form elements:
2011-06-06 15:50:35 +00:00
2012-03-14 02:36:09 +00:00
<input ng-model="query" value="fluffy cloud">
2011-06-06 15:50:35 +00:00
The code above creates a model called "query" on the current scope with the value set to "fluffy
cloud".
* An iterator declaration in {@link api/ng.directive:ngRepeat ngRepeater}:
2011-06-06 15:50:35 +00:00
2012-03-14 02:36:09 +00:00
<p ng-repeat="phone in phones"></p>
2011-06-06 15:50:35 +00:00
The code above creates one child scope for each item in the "phones" array and creates a "phone"
object (model) on each of these scopes with its value set to the value of "phone" in the array.
2012-09-26 13:30:55 +00:00
In Angular, a JavaScript object stops being a model when:
2011-06-06 15:50:35 +00:00
2012-09-26 13:30:55 +00:00
* No Angular scope contains a property that references the object.
2011-06-06 15:50:35 +00:00
2012-09-26 13:30:55 +00:00
* All Angular scopes that contain a property referencing the object become stale and eligible for
2011-06-06 15:50:35 +00:00
garbage collection.
The following illustration shows a simple data model created implicitly from a simple template:
<img src="img/guide/about_model_final.png">
## Related Topics
* {@link dev_guide.mvc About MVC in Angular}
* {@link dev_guide.mvc.understanding_controller Understanding the Controller Component}
* {@link dev_guide.mvc.understanding_view Understanding the View Component}