[AngularJS]Directives and Expressions

AngularJS is a JavaScript framework.

AngularJS directives

AngularJS directives are HTML attributes with an ng prefix.

  • ng-app directive defines an AngularJS application and the root element of an AngularJS application. It will auto-bootstrap (automatically initialize) the application when a web page is loaded.

  • ng-model directive binds the value of HTML controls (input, select, textarea) to application data.

  • ng-bind directive binds application data to the HTML view.

Example: the letter you type in the input box will appear in

simultaneously.






Input something in the input box:

Name:

  • ng-init directive initializes AngularJS application variables.
    Example: display is "The name is John"

The name is

You can use data-ng-, instead of ng-, if you want to make your page HTML valid.

AngularJS Expressions

Tow way to use expressions:

  1. inside double braces: {{ expression }}.
  2. inside a directive: ng-bind="expression".

Without the ng-app directive, HTML will display the expression as it is, without solving it.



//My first expression: 10

My first expression: {{ 5 + 5 }}

//My first expression: {{ 5 + 5 }}

My first expression: {{ 5 + 5 }}

It is the same using ng-bind:

The name is {{ person.lastName }}

The name is

AngularJS Expressions vs. JavaScript Expressions

Like JavaScript expressions, AngularJS expressions can contain literals, operators, and variables.

Unlike JavaScript expressions, AngularJS expressions can be written inside HTML.

AngularJS expressions do not support conditionals, loops, and exceptions, while JavaScript expressions do.

AngularJS expressions support filters, while JavaScript expressions do not.

你可能感兴趣的:([AngularJS]Directives and Expressions)