See below example html.
<div ng-app ng-init="qty=1;cost=2">
<b>Invoice:</b>
<div>
Quantity: <input type="number" ng-model="qty">
</div>
<div>
Costs: <input type="number" ng-model="cost">
</div>
<div>
<b>Total:</b> {{qty * cost | currency}}
</div>
</div>
The AngularJS involved HTML looks like normal HTML, with some new markup. In Angular, a file like this is called a "template". When Angular starts your application, it parses and processes this new markup from the template using the so called "compiler". The loaded, transformed and rendered DOM is then called the "view".
The first kind of new markup are the so called "directives". They apply special behavior to attributes or elements in the HTML.
The second kind of new markup are the double curly braces
{{ expression | filter }}: When the compiler encounters this markup, it will replace it with the evaluated value of the markup. An "expression" in a template is a JavaScript-like code snippet that allows to read and write variables. Note that those variables are not global variables. Just like variables in a JavaScript function live in a scope, Angular provides a "scope" for the variables accessible to expressions. The values that are stored in variables on the scope are referred to as the"model" in the rest of the documentation. Applied to the example above, the markup directs Angular to "take the data we got from the input widgets and multiply them together".
The example above also contains a "filter". A filter formats the value of an expression for display to the user. In the example above, the filter
currency formats a number into an output that looks like money.
The important thing in the example is that angular provides live bindings: Whenever the input values change, the value of the expressions are automatically recalculated and the DOM is updated with their values. The concept behind this is "two-way data binding".
No comments:
Post a Comment