Home Web Front-end JS Tutorial Getting started with Angularjs basics_AngularJS

Getting started with Angularjs basics_AngularJS

May 16, 2016 pm 04:24 PM
angularjs Base

Regarding this, I actually don’t know who I should target or where to start writing, so here I will start writing according to a simple idea

1.angular.element
2.angular.Bootstrap

We know very well that when ng-app is applied to a node, angular will automatically initialize it for you. The initialization process is divided into the following steps

1. Angular will automatically initialize during document load. It will first find the node specified by the ng-app instruction.
2. Load module-related instructions
3. Create an injector (dependency manager) related to the application
4. With the specified ng-app as the root node, start compiling the Dom

Now let’s initialize it ourselves and make something equivalent to the ng-app command. angular.element is a wrapper that wraps original DOM elements or HTML strings as jQuery elements. angular.Bootstrap can manually initialize the script. We use these two to initialize this script

Copy code The code is as follows:





Bootstrap-manual



This is outside ng-app~~{{1 2}}
This is inside ng-app~~~ {{1 2}}





2.compiler

We clearly see that the official documents of angularjs are all camel case nomenclature, such as ngApp, ngModule, ngBind, etc. These are related instructions. The html compiler allows us to define element attributes and tags ourselves. Angular will These additional behaviors are called directives.
The official documentation explains the compiler as follows

Copy code The code is as follows:

Compiler
Compiler is an Angular service which traverses the DOM looking for attributes. The compilation process happens in two phases.

Compile: traverse the DOM and collect all of the directives. The result is a linking function.

Link: combine the directives with a scope and produce a live view. Any changes in the scope model are reflected in the view, and any user interactions with the view are reflected in the scope model. This makes the scope model the single source of truth.

Some directives such as ng-repeat clone DOM elements once for each item in a collection. Having a compile and link phase improves performance since the cloned template only needs to be compiled once, and then linked once for each clone instance.

Compiler is a service of Angular, responsible for traversing DOM nodes and searching for attributes. Compilation is divided into two stages:

1. Compile: traverse the nodes and collect all directives, and return a linking function
2. Link: Bind directives to a scope and create a live view. Any changes in the scope will be reflected in the view (updating the view); any user activity (change) on the template will be reflected in the scope model (two-way binding). This allows the scope model to reflect the correct values.
Some directives, such as ng-repeat, will copy a specific element (combination) once for each element in the collection. The two stages of compilation and linking improve performance. Because the cloned template only needs to be compiled once and then linked once for each element in the collection (similar to template caching).

3. Create your own directive step by step

1. Understand directive
First of all, understand that directives follow camel case naming, such as ngModule. When compiled, the matching is like this, for example:

Copy code The code is as follows:



directive can use x- or data- as the prefix, and can use delimiters such as:, -, or _ to convert camel case naming methods, as shown below:

Copy code The code is as follows:











Generally we use ng-bind to correspond to ngBind, this format
$compile can match directives based on element names, attributes, class names and comments

Copy code The code is as follows:





During the compilation process, the compiler matches embedded expressions (such as {{something}}) in text and attributes through the $interpolate service. These expressions will be registered as watches and updated as part of the digest cycle. Here is a simple interpolation:
Hello {{username}}!

2. Compilation steps

Three steps of HTML "compilation":

1. First, convert HTML into DOM objects through the browser’s standard API. This is an important step. Because the template must be parsable (conforming to the specification) HTML. This can be compared with most template systems, which are generally based on strings rather than DOM elements.
2. Compilation of the DOM is completed by calling the $comple() method. This method traverses the DOM and matches the directive. If the match is successful, it will be added to the directive list together with the corresponding DOM. As long as all directives associated with the specified DOM are identified, they will be sorted by priority and their compile() functions will be executed in this order. The compile function of the directive has an opportunity to modify the DOM structure and is responsible for generating the parsing of the link() function. The $compile() method returns a combined linking function, which is a collection of linking functions returned by all directive's own compile functions.
3. Connect the template to the scope through the linking function returned in the previous step. This in turn calls the directive's own linking function, allowing them to register some listeners on the element and set up some watches in conjunction with the scope. The result is a two-way, instant binding between the scope and the DOM. When the scope changes, the DOM will get the corresponding response.

Copy code The code is as follows:

var $compile = ...; // injected into your code
var scope = ...;
var html = '
';
// Step 1: parse HTML into DOM element
var template = angular.element(html);
// Step 2: compile the template
var linkFn = $compile(template);
// Step 3: link the compiled template with the scope.
linkFn(scope);

ngAttr attribute binding

Copy code The code is as follows:




That’s all for today, and I’ll start writing and creating the directive tomorrow ~~~ Don’t keep the length too long, there are many main concepts in this chapter~~~

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)

PHP Basics Tutorial: From Beginner to Master PHP Basics Tutorial: From Beginner to Master Jun 18, 2023 am 09:43 AM

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

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

Can I learn Linux from scratch? What do I need to learn? Can I learn Linux from scratch? What do I need to learn? Feb 19, 2024 pm 12:57 PM

If you want to work in the IT industry, but do you want to learn programming, which technology should you choose? Of course it is Linux operation and maintenance. Linux is a very popular technology on the market, with a wide range of applications and good employment prospects, and is liked by many people. So the question is, can I learn Linux operation and maintenance with zero basic knowledge? In the server market, Linux system has a market share of up to 80% because of its advantages such as stability, security, free open source, efficiency and convenience. From this, it can be seen that Linux applications are very popular. Extensive. Whether now or in the future, learning Linux is a very good choice. As for whether it is possible to learn from scratch? My answer is of course. Oldboy Education Linux face-to-face class is specially designed for people with zero basic knowledge

Learn the basics of Go language variables Learn the basics of Go language variables Mar 22, 2024 pm 09:39 PM

Go language is a statically typed, compiled language developed by Google. Its concise and efficient features have attracted widespread attention and love from developers. In the process of learning the Go language, mastering the basic knowledge of variables is a crucial step. This article will explain basic knowledge such as the definition, assignment, and type inference of variables in the Go language through specific code examples to help readers better understand and master these knowledge points. In the Go language, you can use the keyword var to define a variable, which is the format of the var variable name variable type.

Introduction to PHP Basics: How to use the echo function to output text content Introduction to PHP Basics: How to use the echo function to output text content Jul 30, 2023 pm 05:38 PM

Basic introduction to PHP: How to use the echo function to output text content. In PHP programming, you often need to output some text content to a web page. In this case, you can use the echo function. This article will introduce how to use the echo function to output text content and provide some sample code. Before starting, first make sure you have installed PHP and configured the running environment. If PHP is not installed yet, you can download the latest stable version from the PHP official website (https://www.php.net).

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

See all articles