aboutsummaryrefslogtreecommitdiffstats
path: root/docs/content/guide/dev_guide.mvc.understanding_model.ngdoc
blob: 1f4715c1be4460ebc25b9387c698be13901f5cdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@ngdoc overview
@name Developer Guide: About MVC in Angular: Understanding the Model Component
@description

Depending on the context of the discussion in the Angular documentation, the term _model_ can refer to
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).

In Angular, a model is any data that is reachable as a property of an angular {@link
scope Scope} object. The name of the property is the model identifier and the value is
any JavaScript object (including arrays and primitives).

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
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) {
             // create property 'foo' on the MyCtrl's scope
             // and assign it an initial value 'bar'
             $scope.foo = 'bar';
         }

* Use an {@link expression angular expression} with an assignment operator in templates:

         <button ng-click="{{foo='bar'}}">Click me</button>

* Use {@link api/ng.directive:ngInit ngInit directive} in templates (for toy/example apps
only, not recommended for real applications):

         <body ng-init=" foo = 'bar' ">

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:

         <input ng-model="query" value="fluffy cloud">

   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}:

         <p ng-repeat="phone in phones"></p>

   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.

In Angular, a JavaScript object stops being a model when:

* No Angular scope contains a property that references the object.

* All Angular scopes that contain a property referencing the object become stale and eligible for
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}
f='#n163'>163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278