search
HomeWeb Front-endJS TutorialSummary of common knowledge for getting started with AngularJS

Summary of common knowledge for getting started with AngularJS

Jul 23, 2017 pm 03:07 PM
angularjsjavascriptbasic 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 })

##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:

section>

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!




②ng-show
Displayed by default when true is passed in

Whether
show?

> ;

③ng-hide
Default Show that the incoming true is hidden

Is it hidden?

> ;

##④ng-click
defines the click event in AngularJS .
Only properties and methods bound in the Angular scope can be triggered.

{{ 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
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
Understanding the JavaScript Engine: Implementation DetailsUnderstanding the JavaScript Engine: Implementation DetailsApr 17, 2025 am 12:05 AM

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python vs. JavaScript: The Learning Curve and Ease of UsePython vs. JavaScript: The Learning Curve and Ease of UseApr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python vs. JavaScript: Community, Libraries, and ResourcesPython vs. JavaScript: Community, Libraries, and ResourcesApr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

From C/C   to JavaScript: How It All WorksFrom C/C to JavaScript: How It All WorksApr 14, 2025 am 12:05 AM

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

See all articles

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)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools