03_Data Binding數據綁定

Data-binding in Angular apps is the automatic synchronization of data between the model and view components. The way that Angular implements data-binding lets you treat the model as the single-source-of-truth in your application. The view is a projection of the model at all times. When the model changes, the view reflects the change, and vice versa.

在Angular應用程序中的數據綁定是在模型和視圖組件之間的數據的自動同步。Angular實現數據綁定的方式是讓你們處理模型在你的應用程序中作爲單一可信的來源。視圖是模型在任何時候的投影。當模型改變了,視圖也會反應變化,反之亦然。

DataBinding in Classical Template Systems

數據綁定在經典模板系統


Most templating systems bind data in only one direction: they merge template and model components together into a view. After the merge occurs, changes to the model or related sections of the view are NOT automatically reflected in the view. Worse, any changes that the user makes to the view are not reflected in the model. This means that the developer has to write code that constantly syncs the view with the model and the model with the view.

大多數模板系統的數據綁定只是單一方向的:他們合併模板和模型組件傳入一個視圖。在合併發生後,更改模型或視圖的相關部分將不會自動反映在視圖中。更糟的是,用戶對視圖的任何更改都不會反映在模型中。這意味着,開發人員不得不經常去編寫模型與視圖和試圖與模型同步的代碼。

DataBinding in Angular Templates

在Angular模板中的數據綁定


Angular templates work differently. First the template (which is the uncompiled HTML along with any additional markup or directives) is compiled on the browser. The compilation step produces a live view. Any changes to the view are immediately reflected in the model, and any changes in the model are propagated to the view. The model is the single-source-of-truth for the application state,greatly simplifying the programming model for the developer. You can think ofthe view as simply an instant projection of your model.

Angular模板的工作方式不同。首先,模板(連同任何額外的標記或指令的未編譯的HTML)編譯在瀏覽器上。編譯過程產生了一個實時的視圖。視圖中的任何更改被立即反映到模型上,模型中的任何更改將傳播到視圖上。模型是單一可信的來源對於應用程序的狀態,對於開發者來說極大地簡化了編程模型。你可以認爲視圖能夠簡化的作爲你的模型的一個即時投影。

 

Because the view is just a projection of the model, the controller is completely separated from the view and unaware of it. This makes testing a snap because it is easy to test your controller in isolation without the view and the related DOM/browser dependency.

因爲視圖只是一個模型的投影,所以控制器能夠從視圖完全分離出來並且不清楚視圖。這使得測試變得輕而易舉,因爲它很容易單獨測試你的控制器在沒有視圖和DOM/瀏覽器依賴的情況下。

Related Topics

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章