This article is an advanced study of Angular. Let’s learn more about routing and forms in Angular. I hope it will be helpful to everyone!
Angular routing introduction
In a single-page application, you need to switch back and forth between different views (components) , instead of getting a new page from the server, to implement navigation from one view to another, you need to use Router in Angular. [Related tutorial recommendations: "angular tutorial"]
Scenarios that require route jump:
If there are two on the page Jump links, we hope that when clicking these links, you can jump to the product list page and personal center page respectively. We can first create two components, GoodsListComponent and PersonalCenterComponent. The specific process is as follows:
Routing creation
1. Create a new file app-routing.module.ts, which will be redirected Put the view configuration inside
Define your routes in the Routes array (Each route in this array is a JavaScript object containing two properties. Chapter One attribute path defines the URL path of the route. The second attribute component defines the components that need to be displayed under the corresponding path)
2. Import AppRoutingModule# in app.module.ts ##
import { AppRoutingModule } from './app-routing.module';
Copy after login
3. Add these routes in app.component.html to control the display of navigation
tag to the template. This element notifies Angular that you can update your app's view with the selected route's components. The usage is the same as that of components. It serves as a placeholder to mark the position where the router should display the component.
Introduction to two important APIs in routing
ActivatedRoute
Used for
GetGetRoute Information fromInformation, it contains routing information such as: routing parameters, static data... For specific methods, please refer to ActivatedRoute official website
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; // ① 先引入ActivatedRoute
@Component({
selector: 'app-goods-list',
templateUrl: './goods-list.component.html',
styleUrls: ['./goods-list.component.scss']
})
export class GoodsListComponent implements OnInit {
constructor(
private route: ActivatedRoute, // ② 依赖注入这个服务
) {}
// ③ 在初始化的生命周期中去或者url的路由信息
public ngOnInit(): void {
// 第一种获取参数的方式
const params = this.route.snapshot.params;
// 第二种获取参数的方式
this.route.params.subscribe(params => {
console.log(params);
});
}
}
Copy after login
The difference and use of the above two methods of obtaining parameters:
route.snapshot:
Needs direct access to parameters, mainly to obtain the
initial value, used without subscription snapshot;
When the application becomes complex, you may need to create some relative routes outside the root component. These routes become sub-routes, which means that a second # needs to be added to the project. ##<router−outlet>##, because it is another outside of AppComponent ##{}##<router##−outlet>.
this.profileForm = new FormGroup({
name: new FormControl('', [Validators.required]),
password: new FormControl(''),
// 在表单中嵌套一个address的表单组
address: new FormGroup({
street: new FormControl(''),
city: new FormControl(''),
})
});
import { Validators } from '@angular/forms';
// angular提供的内置验证器主要有: min max required email minLength maxLength pattern...
Copy after login
2、把这个验证器添加到表单中的相应字段。
场景:需要做验证的内容分别是,① 名称不能为空; ② 密码不能为空,且最小长度不能少于4; ③住址中的街道不能为空
this.profileForm = new FormGroup({
name: new FormControl('', [Validators.required]),
password: new FormControl('', [Validators.required, Validators.minLength(4)]),
address: new FormGroup({
street: new FormControl('', [Validators.required]),
city: new FormControl(''),
})
});
The above is the detailed content of Angular advanced learning: in-depth understanding of routing and forms. 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
How to implement page jump after PHP form submission [Introduction] In web development, form submission is a common functional requirement. After the user fills out the form and clicks the submit button, the form data usually needs to be sent to the server for processing, and the user is redirected to another page after processing. This article will introduce how to use PHP to implement page jump after form submission. [Step 1: HTML Form] First, we need to write a page containing a form in an HTML page so that users can fill in the data that needs to be submitted.
Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub
How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: <!DOCTYP
How to use JavaScript to implement real-time verification of the input box content of a form? In many web applications, forms are the most common way of interaction between users and the system. However, the content entered by the user often needs to be validated to ensure the accuracy and completeness of the data. In this article, we will learn how to use JavaScript to implement real-time verification of the content of the form's input box and provide specific code examples. Creating the form First we need to create a simple table in HTML
The default display behavior for components in the Angular framework is not for block-level elements. This design choice promotes encapsulation of component styles and encourages developers to consciously define how each component is displayed. By explicitly setting the CSS property display, the display of Angular components can be fully controlled to achieve the desired layout and responsiveness.
Apache Camel is an Enterprise Service Bus (ESB)-based integration framework that can easily integrate disparate applications, services, and data sources to automate complex business processes. ApacheCamel uses route-based configuration to easily define and manage integration processes. Key features of ApacheCamel include: Flexibility: ApacheCamel can be easily integrated with a variety of applications, services, and data sources. It supports multiple protocols, including HTTP, JMS, SOAP, FTP, etc. Efficiency: ApacheCamel is very efficient, it can handle a large number of messages. It uses an asynchronous messaging mechanism, which improves performance. Expandable
How to use HTML, CSS and jQuery to implement the advanced function of automatic saving of forms. Forms are one of the most common elements in modern web applications. When users enter form data, how to implement the automatic saving function can not only improve the user experience, but also ensure data security. This article will introduce how to use HTML, CSS and jQuery to implement the automatic saving function of the form, and attach specific code examples. 1. Structure of HTML form. Let’s first create a simple HTML form.
Authentication is one of the most important parts of any web application. This tutorial discusses token-based authentication systems and how they differ from traditional login systems. By the end of this tutorial, you will see a fully working demo written in Angular and Node.js. Traditional Authentication Systems Before moving on to token-based authentication systems, let’s take a look at traditional authentication systems. The user provides their username and password in the login form and clicks Login. After making the request, authenticate the user on the backend by querying the database. If the request is valid, a session is created using the user information obtained from the database, and the session information is returned in the response header so that the session ID is stored in the browser. Provides access to applications subject to