官术网_书友最值得收藏!

Scope

Data binding in AngularJS is achieved using the scope object. We can attach properties to it and explicitly declare in the template that we want to bind to these properties (one- or two-way). Although the idea of the scope seems clear, it has two more responsibilities, including event dispatching and the change detection-related behavior.

Angular beginners have a hard time understanding what scope really is and how it should be used. AngularJS 1.2 introduced something called controller as syntax. It allows us to add properties to the current context inside the given controller (this), instead of explicitly injecting the scope object and later adding properties to it. This simplified syntax can be demonstrated through the following snippet:

<div ng-controller="MainCtrl as main"> 
  <button ng-click="main.clicked()">Click</button> 
</div>
function MainCtrl() { 
  this.name = 'Foobar'; 
} 
MainCtrl.prototype.clicked = function () { 
  alert('You clicked me!'); 
}; 

The latest Angular took this even further by removing the scope object. All the expressions are evaluated in the context of the given UI component. Removing the entire scope API introduces higher simplicity; we don't need to explicitly inject it anymore, instead, we add properties to the UI components to which we can later bind. This API feels much simpler and more natural.

We will take a more detailed look at the components and the change detection mechanism of Angular in Chapter 5, Getting Started with Angular Components and Directives.

主站蜘蛛池模板: 衡南县| 金堂县| 长汀县| 清涧县| 舞钢市| 涪陵区| 西和县| 齐河县| 灵山县| 新安县| 泸水县| 阳新县| 白朗县| 长泰县| 莱州市| 安新县| 石棉县| 磐安县| 蚌埠市| 香河县| 马尔康县| 正安县| 亳州市| 哈巴河县| 庆城县| 舟曲县| 普陀区| 江城| 石狮市| 琼海市| 和硕县| 福清市| 垦利县| 汉源县| 贵定县| 南华县| 上杭县| 保康县| 望谟县| 白玉县| 闽清县|