Home Web Front-end JS Tutorial Summary of common knowledge for getting started with AngularJS

Summary of common knowledge for getting started with AngularJS

Jul 23, 2017 pm 03:07 PM
angularjs javascript basic knowledge

Preface

Let’s learn about AngularJS with you today...

AngularJS uses new attributes and expressions The format extends HTML.

AngularJS can build a single page application.

AngularJS is very easy to learn.

## 1. AngularJS instructions and expressions

【AngularJS common instructions】
1. ng-app: declare that Angular is governed area is generally written on the body or HTML. In principle, there is only one on a page.
2. ng-model: Bind the element value (such as the value of the input field) to the application variable.
#eg:
3. ng-bind: Put the Data is bound to the HTML view and can be replaced by expressions.
eg:


is equivalent to
{{name}}

4. ng-init: Initialize AngularJS application variables.
eg:
5. Expression: {{} }Bind expression, which can contain literals, operators and variables.
But the expression will see {{}} when the web page is loaded, so you can use ng-bind="" instead.
eg:{{ 5 + "" + 5 + ',Angular' }}

[Basic concepts]
Directive: In AngularJS, functionality is provided by extending HTML attributes.
Therefore, the new attributes starting with ng- are called instructions by us.

[MVC three-tier architecture]
1. Model:
The part of the application used to process data. (Save or modify data to database, variables, etc.). Model in AngularJS specifically refers to: data.
View (View): The page that the user sees for displaying data.
Controller: The part of the application that handles user interaction. Responsible for reading data from the view, controlling user input, and sending data to the model.

2. Working principle:
The user sends a request from the view layer. After receiving the request, the controller forwards it to the corresponding model for processing, and returns after the model processing is completed. The result is given to the controller and fed back to the user at the View layer.

3. Create an Angular module, which is the part bound by ng-app. Two parameters need to be passed:
①Module name: the name that ng-app needs to be bound to, ng-app="myApp"
②Array: the name of the module that needs to be injected, not required null.
eg:var app= angular.module("myApp",[]);

On the Angular module, create a controller Controller, required Pass two parameters.
①Controller name, that is, the name that ng-controller needs to be bound to. ng-controller="myCtrl"
②Controllerd's constructor: The constructor can pass in multiple parameters, including $scope/$rootScope and various system built-in objects;

[Scope in AngularJS]
①$scope: local scope, properties and methods declared on $scope can only be used in the current Controller
②$rootScope: Root scope, properties and methods declared on $rootScope,
can be used in any area included in ng-app (whether with Controller or not, Or whether it is in the scope of the Controller)

##>>>If $scope is not used to declare variables, and the variable scope bound using ng-model directly in HTML is :
1. If ng-model is in an ng-controller, this variable will be bound to the $scope of the current Controller by default;
2. If ng-model is not in any ng-controller, this variable will be bound to $rootScope.

# 2. Scope in MVC in AngularJS

In AngularJS, filters can be added to expressions and directives using a pipe character (|).

>>>System built-in filter:
currency: Format numbers into currency format.
filter: Select a subset from array items.
lowercase: Format the string to lowercase.
orderBy: Arrange the array according to an expression.
uppercase: Format the string to uppercase.

eg:

{{"aBcDeF"|uppercase}}


{{"aBcDeF"|lowercase}}


{{123456|currency}}

【Custom filter】

1 .filter('reverse',function(){ //可以注入依赖2 return function(text){3 if(!angular.isString(text)){4 return "您输入的不是字符串!"5 }else{6 return text.split("").reverse().join("");7 }8 }9 })
Copy after login

##3. AngularJS filter
##

1. http in AngularJS
$http is a core service in AngularJS, used to read data from remote servers.

2. Select in AngularJS
① Use an array as the data source, where x represents each item of the array.
By default, x will be directly bound to the value of option, and the content displayed by option is determined by the previous x for....
eg:

② Use objects as data sources, where (x, y) represents key-value pairs, x is the key and y is the value.
By default, the value y will be bound to the value of option, and the content displayed by option is determined by the previous x for....
eg:

3. DOM operations in AngularJS
①ng-disabled="true/false"
When passing When true, the control is disabled. When false is passed in, enabled.

Do you agree
Xiaoxi is so cute!


{{ count }}


4. http && select && DOM operations in AngularJS
五、AngularJS中的表单验证

1、表单中常见的验证操作:
$dirty:表单有填写记录
$valid:字段内容合法的
$invalid:字段内容是非法的
$pristine:表单没有填写记录
$error:表单验证不通过的错误信息

2、验证时需给表单及需要验证的input,设置name属性;
给form及input设置name后,会将form表单信息,默认绑定到$scope作用域中,故可以使用formName.inputName.$验证操作 得到验证结果;
eg:
formName.inputName.$dirty="true" 表单被填写过
formName.inputName.$invalid="true" 表单输入不合法
formName.inputName.$error.required="true" 表单必填但未填
$error支持的验证有:required/minlength/maxlength/pattern/email/number/data
/url等……

3、为避免冲突,例如使用type="email"时,H5也会进行验证操作。
如果只想使用AngularJS验证,可以使用

属性,禁用H5自带验证功能。

 

六、AngularJS中的动画 

Using animation in AngularJS:
provides animation effects and can be used with CSS.

1. To use animation in AngularJS, you need to introduce the angular-animate.js library!

2. If there is no custom module (ng-app) in the page, you can directly bind the system module ng-app="ngAnimate";
If there is already a custom module in the page, you can inject the "ngAnimate" module after the custom module.
eg:angular.module("app",["ngAnimate"])

3. When the relevant instructions are called to control the display and hiding of elements, it will Automatically add the corresponding class;
ng-show/ng-hide will remove/add ng-hide
ng-if/ng-switch/ng- For other instructions such as repeat, you need to set the class style after display and hide respectively;
After display: .ng-enter-active,.ng-leave{}
After hiding: .ng-enter,.ng-leave-active{}



1. Load the js file that implements routing: angular-route.js.

2. Contains the ngRoute module as a dependent module of the main application module.
eg:angular.module("app",["ngRoute"])

3. Change the hyperlink to a path format:
eg:page1

4. In config, inject $routeProvider for routing Configuration:
$routeProvider
.when('/',{template:'This is the home page'})
. when('/page1',{template:'This is page1'})
.when('/page2',{template:'This is page2'})
.when('/page3',{template:'This is page3 page'})
.otherwise({redirectTo:'/'});
})
5. Add ng-view at the appropriate position on the page to host the opened page

[Optional attributes in routing parameter object]
1. tempalte: Custom HTML template, which will be loaded in ng-view
2.tempalteUrl: Import the external HTML template. In order to avoid conflict with the external HTML, you only need to keep the code inside the body;
3.redirectTo: Redirect to a certain page, generally used in .otherwise();
4.controller: The controller function executed on the current template , generate a new scope

Today’s theoretical knowledge will be shared here first, I hope it can help you~~~ I am a newbie, thank you for your support!




Author: Xizhao Hope
Source:
##7. Routing in AngularJS

The above is the detailed content of Summary of common knowledge for getting started with AngularJS. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap

Video Face Swap

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

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)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles