Prerequisite: Prepare an empty Angular project (ng new Angular-course)
First meeting with an Angular component
- ** By creating the component command: **
**ng g c components/HelloWorld**
To generate our first component - Watch for directory changes and generate our component-related files under SRC /app/ Components
hello-world.component.html
The content to be displayed by the componenthello-world.component.scss
Component style definition, CSS precompiler can be pre-selected when creating a projecthello-world.component.ts
Component core classhello-world.component.spec.ts
Group unit test use- Let’s open it up
**hello-world.component.ts**
The ** component core class takes a look at the content, in addition to the regular import module and creating a ****HelloWorldComponent**
Class, is also used**@Component**
A decorator.
selector
: Annotates the name of the component, which is used when the component is usedtemplateUrl
: Path to annotate the HTML templatestyleUrls
: The path to the style used by the HTML template. We see that the array format is used, indicating that passing multiple style files should not be a problem- Component files are for now. Components created during Vue development always need to be mounted. Does Angular need to mount them? How do you do that?
- Because git repositories are initialized by default when angular projects are created, we can now see another file that has changed
app.module.ts
When I opened the file, I saw that the newly created component had been automatically mounted globallyapp
On.
- Because git repositories are initialized by default when angular projects are created, we can now see another file that has changed
- Now let’s go through the modification
**app.component.html**
Content to show our own component- empty
app.component.html
The content of the - Remember the name of the component we created? We want to display the component to
app
Inside:<app-hello-world></app-hello-world>
- empty
- Start the project
**ng serve --open**
You can see that the page is displaying**hello-world works! 六四运动
This is what is automatically generated when our component is created.
Enrich our components
Binding properties
- Syntax example: <img [
attribute
] =”variable
“/ > - Add the imgUrl property to the component’s ts file:
public imgUrl: string = 'assets/logo.png';
- Use this in a component’s HTML template:
<img [src]="imgUrl" alt="">
- Take a look at our page
The binding event
- Syntax example: <button (
event
)=”fun($event)” - Add it to the component’s TS file
print
function
print(event: Event) {
console.log('[ print ] >>'.'hello world'.'Event type:' + event.type);
}
Copy the code
- Trigger by adding a button to the component’s HTML template
print
function
<button (click)="print($event)"</button>Copy the code
Two-way binding
- Bidirectional binding: attribute binding + event binding, so bidirectional binding syntax is [(
attribute
)] =”variable
“ - We prepare a component that demonstrates bidirectional binding:
ng g c components/sizer
- Component that we need to pass through
@Inout()
Decorator to receive data through@Output
Decorator to distribute data to achieve two-way data flow - Component TS code (from Angular Chinese):
- Component that we need to pass through
export class SizerComponent { @Input() size! : number | string; @Output() sizeChange =new EventEmitter<number>();
dec() { this.resize(-1); }
inc() { this.resize(+1); }
resize(delta: number) {
this.size = Math.min(40.Math.max(8, +this.size + delta));
this.sizeChange.emit(this.size); }}Copy the code
- Component HTML template code (from Angular Chinese):
<div>
<button (click) ="dec()" title="smaller">-</button>
<button (click) ="inc()" title="bigger">+</button>
<label [style.font-size.px] ="size">FontSize: {{size}}px</label>
</div>
Copy the code
- We will demonstrate bidirectional binding by mounting components to
app
And, inapp
Declare the fontSizePx variable in the component
<app-sizer [(size)] ="fontSizePx"></app-sizer>
<div [style.font-size.px] ="fontSizePx">Resizable Text</div>
Copy the code
- You can see that in the demo
app
In thefontSizePx
Property is passed tosizer
In the component- In the operating
sizer
Component after will againsize
Update to theapp
In thefontSizePx
Property, and the page updates accordingly
Structural instruction
- NgIf(built-in) :
- Add attributes to component TS:
public isShow: boolean = true;
- Add demo code to the component HTML template:
- Add attributes to component TS:
<button (click) ="isShow = ! isShow">{{ isShow ? "Do not display" : "display"}}</button>
<p *ngIf="isShow">Hello World</p>
Copy the code
- NgFor(built-in) :
- Add attributes to component TS:
Public list: Array<String> = [' myarray ', 'Huawei ',' Apple '];
- Add demo code to the component HTML template:
- Add attributes to component TS:
<div *ngFor="let item of list; let i = index">
{{ i }} {{ item }}
</div>
Copy the code
- NgSwitch(built-in) :
- Add attributes to component TS:
public status: number = 1;
- Add demo code to the component HTML template:
- Add attributes to component TS:
<div [ngSwitch] ="status">
<div *ngSwitchCase="0">Waiting for the</div>
<div *ngSwitchCase="1">Has been completed</div>
<div *ngSwitchDefault>The unknown</div>
</div>
Copy the code
- Features:
- One tag and one structural instruction
- * mark
Attribute instruction
- NgClass
- In the component’s style file add:
.class1{
background-color: chocolate;
}
.class2{
width: 100px;
height: 50px;
}
.class3{
font-size: 20px;
color: chartreuse;
}
Copy the code
- Some styles can be optionally turned on and off by variables when binding in a component’s HTML template
<div [ngClass] ="{ class1: false, class2: true, class3: true }">Try binding a set of classes</div>
Copy the code
- NgStyle
Try binding a set of styles to a component HTML template
<div [ngStyle] ="{ 'background-color': 'chocolate', width: '150px' }">Try binding a set of styles</div>
Copy the code
- NgModel(for form elements only)
- Add attributes to component TS:
public value: string = 'hello world';
- Add the demo code to the component’s HTML:
- Add attributes to component TS:
<input type="text" [(ngModel)] ="value">
<p>value: {{value}}</p>
Copy the code
- Attention needs to be paid to
module
In the importFormsModule
Otherwise, the function cannot be implemented - When the page is restored, you can update the content in the input box to update the data bound to the page
The pipe
- Pipes in Angular are similar to filters in Vue in that they display data in a specified format and operate with pipe characters
- Built-in pipe:
- Presentation:
- Format the time into a uniform style
<div>DatePipe: {{currentTime | date: "yyyy MM ‐ ‐ dd HH: MM: ss"}}</div>
Copy the code
- Convert all strings to lowercase
<div>LowerCasePipe: {{ value | lowercase }}</div>
Copy the code
- Uppercase all strings
<div>UpperCasePipe: {{ value | uppercase }}</div>
Copy the code
- Convert the object into A JSON string and output it on the page for easy debugging
conclusion
In this chapter we mainly demonstrate the basic use of components, can achieve simple functions, the next chapter we will demonstrate communication between components. How many students still use Angular and would not contact it if it were not for their work? I first learned Angualr in 15 years. There is a point ha, the original is pIA PIA paste code, today the first time to describe more, when the exercise, the expression is not also please correct ha.