- Switching to Angular(Third Edition)
- Minko Gechev
- 225字
- 2021-07-02 15:23:30
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.
- FuelPHP Application Development Blueprints
- JavaScript從入門到精通(微視頻精編版)
- 計算機圖形學編程(使用OpenGL和C++)(第2版)
- Access 2010數據庫基礎與應用項目式教程(第3版)
- 軟件架構:Python語言實現
- 快速念咒:MySQL入門指南與進階實戰
- Oracle JDeveloper 11gR2 Cookbook
- Scientific Computing with Scala
- Elasticsearch Essentials
- Learning VMware vSphere
- Getting Started with Electronic Projects
- Moodle 3.x Developer's Guide
- Java EE 程序設計
- 熱處理常見缺陷分析與解決方案
- 零基礎Linux從入門到精通