Angular.js review ng-app and ng-model usage tips_AngularJS
Simple structure of index.html in Angular.js:
<!doctype html> <html ng-app> <head> <script src="http://code.angularjs.org/angular-1.0.1.min.js"></script> </head> <body> Your name: <input type="text" ng-model="yourname" placeholder="World"> <hr> Hello {{yourname || 'World'}}! </body> </html>
ng-app attribute is the flag statement of angular.js, which marks the scope of angular.js. ng-app can be added in many places, like the above added to the html tag, indicating that the angular script works on the entire page. You can also add the ng-app attribute locally. For example, adding ng-app within a certain div will indicate that the entire div area will be parsed using angular scripts, while other locations will not be parsed by angular scripts.
ng-model means building a data model. Here, in the input box for inputting the name, we define a yourname data model. Once the model is defined, we can call it below by leveraging {{}}. This completes the data binding. When we enter content in the input box, it will be synchronized to the Hello statement block below.
The data model defined by ng-model can not only be used in the above scenarios, but can also be widely used in many situations.
1. Set filter to implement search function
In the following code, we use a simple data model definition + filter to complete a list search function. (This is an example code from the Chinese website, so you don’t need to worry about the unclear parts first)
<div class="container-fluid"> <div class="row-fluid"> <div class="span2"> Search: <input ng-model="query"> </div> <div class="span10"> <ul class="phones"> <li ng-repeat="phone in phones | filter:query"> {{phone.name}} <p>{{phone.snippet}}</p> </li> </ul> </div> </div> </div>
In the above code, the data model query is bound to the input tag of the search box. In this way, the information entered by the user will be synchronized to the query data model. In the following li, you can use filter:query to implement the data filtering function in the list and perform filtering based on the user's input information.
2. Set orderBy to implement list sorting function
In the following code, in the same way as filter, orderBy is used to add a sorting function to the list:
Search: <input ng-model="query"> Sort by: <select ng-model="orderProp"> <option value="name">Alphabetical</option> <option value="age">Newest</option> </select> <ul class="phones"> <li ng-repeat="phone in phones | filter:query | orderBy:orderProp"> {{phone.name}} <p>{{phone.snippet}}</p> </li> </ul>
The above is about the usage skills of ng-app and ng-model. I hope you can gain something from it by reviewing the past and learning the new.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article will take you to continue learning angular and briefly understand the standalone component (Standalone Component) in Angular. I hope it will be helpful to you!

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!

The Angular project is too large, how to split it reasonably? The following article will introduce to you how to reasonably split Angular projects. I hope it will be helpful to you!

How to customize the angular-datetime-picker format? The following article talks about how to customize the format. I hope it will be helpful to everyone!

This article will take you through dependency injection, introduce the problems that dependency injection solves and its native writing method, and talk about Angular's dependency injection framework. I hope it will be helpful to you!

The NgModule module is an important point in Angular, because the basic building block of Angular is NgModule. This article will take you through the NgModule module in Angular. I hope it will be helpful to you!

This article will give you an in-depth understanding of several special selectors in Angular: host, :host-context, ::ng-deep. I hope it will be helpful to you!
