Home Web Front-end JS Tutorial AngularJS Basic Learning Notes Instructions_AngularJS

AngularJS Basic Learning Notes Instructions_AngularJS

May 16, 2016 pm 03:59 PM
angularjs instruction

AngularJS extends HTML attributes through directives.

AngularJS Directives
AngularJS directives are extended HTML attributes with the ng- prefix.

The ng-app directive is used to initialize the AngularJS application.

The ng-init directive is used to initialize application data.

The ng-model directive is used to bind the values ​​of HTML controls (such as input, select, textarea, etc.) to application data.

<div ng-app="" ng-init="firstName='John'">

<p>Name: <input type="text" ng-model="firstName"></p>
<p>You wrote: {{ firstName }}</p>

</div>

Copy after login

The ng-app directive also tells AngularJS that the <div> element it is located in is the root element of the AngularJS application, that is, the scope.

Data Binding

In the above example, {{ firstName }} is an AngularJS data binding expression.

In AngularJS data binding, AngularJS expressions use AngularJS data to update synchronously.

 {{ firstName }} updates data synchronously through ng-model="firstName".

&lt;div ng-app="" ng-init="quantity=1;price=5"&gt;

Quantity: &lt;input type="number"  ng-model="quantity"&gt;
Costs:  &lt;input type="number" ng-model="price"&gt;

Total in dollar: {{ quantity * price }}

&lt;/div&gt;

Copy after login

Note Using the ng-init directive is very common in AngularJS development. In the Controllers section you'll see better ways to initialize data.
Repeat HTML element
The ng-repeat directive is used to repeatedly create an HTML element:

&lt;div ng-app="" ng-init="names=['Jani','Hege','Kai']"&gt;
 &lt;ul&gt;
  &lt;li ng-repeat="x in names"&gt;
   {{ x }}
  &lt;/li&gt;
 &lt;/ul&gt;
&lt;/div&gt;
Copy after login

Use the ng-repeat directive on an array of objects:

&lt;div ng-app="" ng-init="names=[
{name:'Jani',country:'Norway'},
{name:'Hege',country:'Sweden'},
{name:'Kai',country:'Denmark'}]"&gt;

&lt;ul&gt;
 &lt;li ng-repeat="x  in names"&gt;
  {{ x.name + ', ' + x.country }}
 &lt;/li&gt;
&lt;/ul&gt;

&lt;/div&gt;

Copy after login

Note AngularJS is very suitable for database CRUD (i.e. create, read, update and delete) operations. Imagine if these objects came from a database?

ng-app directive

The ng-app directive defines the root element of the AngularJS application.

When the Web page is loaded, the ng-app instruction will auto-bootstrap (automatically initialize) the application. That is, automatically initialize and guide the execution of AngularJS application.

In the following chapters you will learn how to assign a value to the ng-app directive (for example, ng-app="myModule") to associate it with the module.

ng-init directive
 The ng-init directive is used to initialize values ​​for AngularJS application.

Generally, there is no need to use the ng-init instruction. Instead, use a controller or module to perform initialization work.

You will learn about controllers and modules in the following chapters.

ng-model directive
The ng-model directive is used to bind the values ​​of HTML controls (such as input, select, textarea, etc.) to application data.

The ng-model directive can also be used:

Provide data verification (such as verification number, email address, required fields).
Provide the status of the data (such as invalid, dirty, touched, error).
Provides CSS style classes for HTML elements.
Bind HTML elements to HTML forms.

ng-repeat directive

The ng-repeat directive is used to generate a corresponding HTML element for each element in the data collection (or array).

The above is the entire content of this article, I hope you all like it.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get items using commands in Terraria? -How to collect items in Terraria? How to get items using commands in Terraria? -How to collect items in Terraria? Mar 19, 2024 am 08:13 AM

How to get items using commands in Terraria? -How to collect items in Terraria?

VUE3 quick start: using Vue.js instructions to switch tabs VUE3 quick start: using Vue.js instructions to switch tabs Jun 15, 2023 pm 11:45 PM

VUE3 quick start: using Vue.js instructions to switch tabs

Explore the infinite possibilities of the input method that comes with MC commands (an innovative tool to create a perfect gaming experience - the input method that comes with MC commands) Explore the infinite possibilities of the input method that comes with MC commands (an innovative tool to create a perfect gaming experience - the input method that comes with MC commands) May 02, 2024 pm 03:01 PM

Explore the infinite possibilities of the input method that comes with MC commands (an innovative tool to create a perfect gaming experience - the input method that comes with MC commands)

The latest 5 angularjs tutorials in 2022, from entry to mastery The latest 5 angularjs tutorials in 2022, from entry to mastery Jun 15, 2017 pm 05:50 PM

The latest 5 angularjs tutorials in 2022, from entry to mastery

What are the two parts of instructions that a computer can directly execute? What are the two parts of instructions that a computer can directly execute? Dec 09, 2020 am 09:15 AM

What are the two parts of instructions that a computer can directly execute?

Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Use PHP and AngularJS to build a responsive website to provide a high-quality user experience Jun 27, 2023 pm 07:37 PM

Use PHP and AngularJS to build a responsive website to provide a high-quality user experience

Build web applications using PHP and AngularJS Build web applications using PHP and AngularJS May 27, 2023 pm 08:10 PM

Build web applications using PHP and AngularJS

Prompt is no longer needed. You can play the multi-modal dialogue system with just your hands. iChat is here! Prompt is no longer needed. You can play the multi-modal dialogue system with just your hands. iChat is here! May 15, 2023 pm 05:55 PM

Prompt is no longer needed. You can play the multi-modal dialogue system with just your hands. iChat is here!

See all articles