AngularJs学习笔记--Understanding the Model Component
来源:互联网 发布:怎样更改域名dns 编辑:程序博客网 时间:2024/06/05 08:43
AngularJs学习笔记--Understanding the Model Component
原版地址:http://docs.angularjs.org/guide/dev_guide.mvc.understanding_model
在angular文档讨论的上下文中,术语“model”可以适用于单一对象代表一个实体(例如,一个叫” phones”的model,它的值是一个电话数组。)或者作为应用的全部数据Model(所有实体)。
在angular中,model可以是任意数据,可以通过angular的scope对象的属性来获取model。属性的名称是model的标识,值可以是任意javascript对象(包括数组和原始数据)。
javascript想成为model的唯一的条件是对象必须作为一个scope对象的属性被angular scope引用。属性的引用关系可以明确或者隐含地创建。
我们可以通过以下几种方式来显式创建scope的属性,关联javascript对象来创建model:
- 在javascript代码中,直接赋值给scope对象的属性;这通常发出现在controller中:
function MyCtrl($scope) { // create property 'foo' on the MyCtrl's scope // and assign it an initial value 'bar' $scope.foo = 'bar'; }
- 在模版的angular表达式(http://www.cnblogs.com/lcllao/archive/2012/09/16/2687162.html)中,使用赋值操作符:
<button ng-click="{{foos='ball'}}">Click me</button>
- 在模版中使用ngInit directive(http://docs.angularjs.org/api/ng.directive:ngInit)(仅仅作为例子,不推荐在真实应用中使用)
<body ng-init=" foo = 'bar' ">
angular在下面的模版结构中会隐式创建model:
- 表单的input 、select、textarea和其他form元素:
<input ng-model="query" value="fluffy cloud">
上面的代码,在当前的scope中创建了一个叫”query”的model,并且与input的value值绑定,初始化为”fluffy cloud”。
- 在ngRepeater中声明迭代器
<p ng-repeat="phone in phones"></p>
上面的代码为每一个phones数组的元素各自创建了一个child scope,并且在对应的child scope中创建”phone”model,赋予数组中对应的值。
在angular中,当出现下面的情况时,javascript对象将不再是一个model:
- 当没有angular scope包含与该对象关联的属性时。
- 所有包含与对象关联的属性的angular scope成为了陈旧和适合垃圾回收时。
下面的插图展示了在一个简单的模版中隐式创建一个简单的数据model。
0 0
- AngularJs学习笔记--Understanding the Model Component
- AngularJs学习笔记--Understanding the Controller Component
- AngularJs学习笔记--Understanding Angular Templates
- 《Understanding the Linux kernel》学习笔记 Chapter 1: Introduction
- 《Understanding the Linux kernel》学习笔记 Chapter 2: Memory Addressing
- 《Understanding the Linux kernel》学习笔记 Chapter 3: Processes
- 《Understanding the Linux kernel》学习笔记 Chapter 7: Process Scheduling
- Inside The C++ Object Model学习笔记
- Understanding ES6学习笔记
- The CORBA Component Model: Part 1, Evolving Towards Component Middleware
- 《Understanding the Linux kernel》学习笔记 Chapter 12: The Virtual Filesystem
- Understanding DB2 学习笔记 一
- Understanding DB2 学习笔记 二
- Understanding DB2 学习笔记 三
- Understanding Component-Entity-Systems
- Inside The C++ Object Model 学习笔记 -- 关于对象
- Inside the C++ Object Model学习笔记[Chap1.1]
- Inside the C++ Object Model学习笔记[Chap1.2]
- 面向对象之对象
- jquery 用alert警告,要加return false
- AngularJs学习笔记--Dependency Injection(DI,依赖注入)
- XCL-Charts画曲线图(CurveChart)
- WebXone V2400 Sp1发布!
- AngularJs学习笔记--Understanding the Model Component
- leetcode第一刷_Remove Duplicates from Sorted Array II
- AngularJs学习笔记--Understanding the Controller Component
- AngularJs学习笔记--E2E Testing
- 稳定排序和不稳定排序
- AngularJs学习笔记--Understanding Angular Templates
- 人脸识别原理检测及实现
- AngularJs学习笔记--Using $location
- CGLib动态代理