Angularjs Understanding the Model Component_angularjs

Source: Internet
Author: User
Tags garbage collection angular scope

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 an array of telephones.) Or as the application of all data model (all entities).

In angular, model can be arbitrary data and can be obtained by angular the properties of the scope object. The name of the property is the identity of 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 objects must be angular scope references as properties of a scope object. A reference relationship to a property can be created either explicitly or implicitly.

There are several ways we can explicitly create a scope property, associating a JavaScript object to create model:

In JavaScript code, a property that is assigned directly to a scope object; This is usually emitted in the controller now:

function Myctrl ($scope) {

   //create property ' foo ' on the Myctrl ' s scope

   //and assign it a initial value ' bar ' 
    $scope. foo = ' bar ';

 }

In the angular expression (http://www.cnblogs.com/lcllao/archive/2012/09/16/2687162.html) of the template, use the assignment operator:

<button ng-click= "{{foos= ' ball '}}" >click me</button>

Use nginit directive (Http://docs.angularjs.org/api/ng.directive:ngInit) in templates (only as an example, not recommended for real applications)

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

Angular will implicitly create model in the following template structure:

Input, select, textarea, and other form elements of the form:

<input ng-model= "Query" value= "Fluffy Cloud" >

The code above creates a model called "query" in the current scope and binds to the value 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 "phone" model in the corresponding child scope, giving the corresponding value in the array.

In angular, the JavaScript object will no longer be a model when the following situation occurs:

When there is no angular scope that contains the properties associated with the object.
All angular scopes that contain properties associated with objects become obsolete and appropriate for garbage collection.

The following illustration shows the implicit creation of a simple data model in a simple stencil.

  above is about Angularjs understanding the Model Component data collation, follow-up continue to add, thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.