Home Web Front-end JS Tutorial AngularJS built-in directive_AngularJS

AngularJS built-in directive_AngularJS

May 16, 2016 pm 04:16 PM
angularjs built-in commands

directive, I understand it as a way for AngularJS to operate HTML elements.
Since the first step in learning AngularJS is to write the built-in directive ng-app to indicate that this node is the root node of the application, the directive is already familiar.

This blog briefly records some built-in commands. Let’s use them first, and then talk about some interesting things.

Built-in commands

All built-in instructions are prefixed with ng. It is not recommended for custom instructions to use this prefix to avoid conflicts.
Start with some common built-in commands.
Let’s first list some key built-in instructions, and briefly talk about scope issues.

ng-model

Binding the form control to the properties of the current scope does not seem to be correct.
But don’t worry about the wording for now, it’s easy to understand when used, for example:

Copy code The code is as follows:



{{someModel.someProperty}}

ng-init

This directive will initialize the inner scope when called.
This command usually appears in relatively small applications, such as giving a demo or something...

Copy code The code is as follows:


I'm a/an {{job}}

In addition to ng-init, we have more and better options.

ng-app

Every time you use AngularJS, you cannot do without this command. By the way, $rootScope.
The element that declares ng-app will become the starting point of $rootScope, and $rootScope is the root of the scope chain, usually declared in you know.
In other words, all scopes under the root can access it.
However, it is not recommended to overuse $rootScope, otherwise global variables will be everywhere, which will be inefficient and difficult to manage.
Here is an example:

Copy code The code is as follows:



{{ someProperty }}

<script><br> var myApp = angular.module('myApp', [])<br> .run(function($rootScope) {<br> $rootScope.someProperty = 'hello computer';<br> }); <br> </script>

ng-controller

We use this command to install a controller on a DOM element.
A controller? Indeed, it is good to understand it literally, so why do we need a controller?
Remember that in AngularJS 1.2.x, you can define controller like this...

Copy code The code is as follows:

function ohMyController($scope) {
//...
}

This method is prohibited in AngularJS 1.3.x, because this method will make the controllers fly all over the sky, and it will be impossible to distinguish the levels. Everything is hung on $rootScope...
ng-controller must have an expression as a parameter. In addition, $scope is used to inherit the methods and properties of the superior $scope, including $rootScope.
The following is just a simple example. The ancestor cannot access the scope of the child.

Copy code The code is as follows:


{{ ancestorName }}
{{ childName }}

             {{ ancestorName }}
             {{ childName }}


<script><br> var myApp = angular.module('myApp', [])<br> .controller('ChildController', function($scope) {<br> $scope.childName = 'child';<br> })<br> .controller('AncestorController', function($scope) {<br> $scope.ancestorName = 'ancestor';<br> });<br> </script>

The problem of scope goes beyond that. Let’s put it aside for now and continue to look at other built-in instructions.

ng-form

At first I didn’t understand why there was a form command, but the

tag seemed useful enough.
Taking form verification as an example, there is this piece of code in the previous article:

Copy code The code is as follows:


That is, the submit button is disabled when the form's status is $invalid.
If the scenario is a little more complicated, for example, a parent form has multiple subforms, and the parent form can be submitted when three verifications in the subforms pass.
However, cannot be nested.
Considering this scenario, we use the ng-form directive to solve this problem.
For example:

Copy code The code is as follows:



Name:

ID number:




Guardian name:

Guardian ID number:



ng-disabled

For attributes like this that are effective as long as they appear, we can make them effective by returning true/false expressions in AngularJS.
Disable form input fields.

Copy code The code is as follows:


ng-readonly

Set the form input field to read-only through the expression return value true/false.
As an example, it will become read-only after 3 seconds.

Copy code The code is as follows:


.run(function($rootScope,$timeout){
$rootScope.stopTheWorld=false;
$timeout(function(){
           $rootScope.stopTheWorld = true;
},3000)
})

ng-checked

This is for , such as...

Copy code The code is as follows:


ng-selected

is used for the

ng-show/ng-hide

Show/hide HTML elements based on expressions. Note that they are hidden, not removed from the DOM, for example:

Copy code The code is as follows:


1 1=2


You can't see me.

ng-change

It’s not onXXX like HTML, but ng-XXX.
Used in conjunction with ng-model, take ng-change as an example:

Copy code The code is as follows:


{{ calc.result }}

or like ng-options

{{}}

In fact, this is also a command. It may feel similar to ng-bind, but it may be seen when the page rendering is slightly slow.
In addition, the performance of {{}} is far inferior to ng-bind, but it is very convenient to use.

ng-bind

The behavior of ng-bind is similar to {{}}, except that we can use this command to avoid FOUC (Flash Of Unrendered Content), which is the flicker caused by unrendering.

ng-cloak

ng-cloak can also solve FOUC for us. ng-cloak will hide internal elements until the route calls the corresponding page.

ng-if

If the expression in ng-if is false, the corresponding element will be removed from the DOM instead of hidden, but when inspecting the element you can see that the expression becomes a comment.
If the phase is hidden, you can use ng-hide.

Copy code The code is as follows:


The element cannot be reviewed


Can be censored

ng-switch

Used alone, it is meaningless. Here is an example:

Copy code The code is as follows:


0


1


2


3



ng-repeat

I don’t understand why it’s not called iterate. In short, it traverses the collection and generates template instances for each element. Some special attributes can be used in the scope of each instance, as follows:

Copy code The code is as follows:

$index
$first
$last
$middle
even
odd

No need to explain, it’s easy to see what these are for. Here is an example:

Copy code The code is as follows:


  • {{char.alphabet}}


ng-href

At first I made an ng-model in a text field, and then wrote in the href like this.
In fact, there is no difference between href and ng-href, so we can try this:

Copy code The code is as follows:


.run(function($rootScope, $timeout) {
$rootScope.linkText = 'Not loaded yet, you cannot click';
$timeout(function() {
          $rootScope.linkText = 'Please click'
          $rootScope.myHref = 'http://google.com';
}, 2000);
})

ng-src

Much the same, that is, do not load the resource before the expression takes effect.
Example (ps: nice picture! ):

Copy code The code is as follows:


.run(function($rootScope, $timeout) {
$timeout(function() {
          $rootScope.imgSrc = 'https://octodex.github.com/images/daftpunktocat-guy.gif';
}, 2000);
})

ng-class

Dynamically change the class style using objects in the scope, for example:

Copy code The code is as follows:




Number is: {{ x }}



.controller('CurTimeController', function($scope) {
$scope.getCurrentSecond = function() {
          $scope.x = new Date().getSeconds();
};
})

The above is all the content described in 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

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

Javascript is a very unique language. It is unique in terms of the organization of the code, the programming paradigm of the code, and the object-oriented theory. The issue of whether Javascript is an object-oriented language that has been debated for a long time has obviously been There is an answer. However, even though Javascript has been dominant for twenty years, if you want to understand popular frameworks such as jQuery, Angularjs, and even React, just watch the "Black Horse Cloud Classroom JavaScript Advanced Framework Design Video Tutorial".

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

In today's information age, websites have become an important tool for people to obtain information and communicate. A responsive website can adapt to various devices and provide users with a high-quality experience, which has become a hot spot in modern website development. This article will introduce how to use PHP and AngularJS to build a responsive website to provide a high-quality user experience. Introduction to PHP PHP is an open source server-side programming language ideal for web development. PHP has many advantages, such as easy to learn, cross-platform, rich tool library, development efficiency

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

With the continuous development of the Internet, Web applications have become an important part of enterprise information construction and a necessary means of modernization work. In order to make web applications easy to develop, maintain and expand, developers need to choose a technical framework and programming language that suits their development needs. PHP and AngularJS are two very popular web development technologies. They are server-side and client-side solutions respectively. Their combined use can greatly improve the development efficiency and user experience of web applications. Advantages of PHPPHP

Changes in Vue3 compared to Vue2: richer built-in instructions Changes in Vue3 compared to Vue2: richer built-in instructions Jul 07, 2023 pm 04:01 PM

Changes in Vue3 compared to Vue2: Richer built-in instructions As time goes by, Vue.js, as a popular JavaScript framework, continues to be upgraded and improved. Vue3 is the latest version of Vue.js, which brings many important changes and upgrades compared to Vue2. One of the most significant changes is the richness of built-in commands. In this article, we will explore some changes in built-in directives in Vue3 compared to Vue2, and provide some code examples to illustrate these changes.

Build a single-page web application using Flask and AngularJS Build a single-page web application using Flask and AngularJS Jun 17, 2023 am 08:49 AM

With the rapid development of Web technology, Single Page Web Application (SinglePage Application, SPA) has become an increasingly popular Web application model. Compared with traditional multi-page web applications, the biggest advantage of SPA is that the user experience is smoother, and the computing pressure on the server is also greatly reduced. In this article, we will introduce how to build a simple SPA using Flask and AngularJS. Flask is a lightweight Py

How to use PHP and AngularJS for front-end development How to use PHP and AngularJS for front-end development May 11, 2023 pm 05:18 PM

With the popularity and development of the Internet, front-end development has become more and more important. As front-end developers, we need to understand and master various development tools and technologies. Among them, PHP and AngularJS are two very useful and popular tools. In this article, we will explain how to use these two tools for front-end development. 1. Introduction to PHP PHP is a popular open source server-side scripting language. It is suitable for web development and can run on web servers and various operating systems. The advantages of PHP are simplicity, speed and convenience

Use PHP and AngularJS to develop an online file management platform to facilitate file management Use PHP and AngularJS to develop an online file management platform to facilitate file management Jun 27, 2023 pm 01:34 PM

With the popularity of the Internet, more and more people are using the network to transfer and share files. However, due to various reasons, using traditional methods such as FTP for file management cannot meet the needs of modern users. Therefore, establishing an easy-to-use, efficient, and secure online file management platform has become a trend. The online file management platform introduced in this article is based on PHP and AngularJS. It can easily perform file upload, download, edit, delete and other operations, and provides a series of powerful functions, such as file sharing, search,

How to use AngularJS in PHP programming? How to use AngularJS in PHP programming? Jun 12, 2023 am 09:40 AM

With the popularity of web applications, the front-end framework AngularJS has become increasingly popular. AngularJS is a JavaScript framework developed by Google that helps you build web applications with dynamic web application capabilities. On the other hand, for backend programming, PHP is a very popular programming language. If you are using PHP for server-side programming, then using PHP with AngularJS will bring more dynamic effects to your website.

See all articles