@Component({
...
template: '<p #bio></p>'
})
export class UserDetailsComponent {
@ViewChild('bio') bio;
}
<h2>Names</h2> <div *ngFor="let user of users | async">{{ user.name }}</div> <h2>Ages</h2> <div *ngFor="let user of users | async">{{ user.age }}</div> <h2>Genders</h2> <div *ngFor="let user of users | async">{{ user.gender }}</div>
export class OrderService {
constructor(private httpClient: HttpClient) {}
addOrder(order: Order) {
// Missing line
}
}
this.httpClient.url(this.orderUrl).post(order);
this.httpClient.send(this.orderUrl, order);
this.httpClient.post<Order>(this.orderUrl, order);
this.httpClient.post<Order>(this.orderUrl, order).subscribe();
@Component({
selector: 'app-user-card',
. . .
})
<div app-user-card></div>
。 <app-user-card></app-user-card>
的第一个实例。 <app-user-card></app-user-card>
的实例。 <user-card></user-card>
的实例。 %OPTION% A
<ul> <li [ngFor]="let productName of productNames">{{ productName }}</li> </ul>
%OPTION% B
<ul> <li ngFor="let productName of productNames">{{ productName }}</li> </ul>
%OPTION% C
<ul> <li *ngFor="let productName of productNames">{{ productName }}</li> </ul>
%OPTION% D
<ul> <? for productName in productNames { ?> <li>{{ productName }}</li> <? } ?> </ul>
@Component({
selector: 'app-title-card',
template: '',
})
class TitleCardComponent {
title = 'User Data';
}
{{ 'title' }}
{{ title }}
[title]
@Component({
selector: 'app-shopping-cart',
. . .
})
export class ShoppingCartComponent {
@Output() itemTotalChanged = new EventEmitter();
}
itemTotalChanged
类字段公开。 itemTotalChanged
类字段的方法,如:<app-shopping-cart [itemTotalChanged] =“newTotal”> </app-shopping-cart>。 itemTotalChanged
类字段的方法,如:<app-shopping-cart (itemTotalChanged) =“logNewTotal($event)”> </app-shopping-cart>。 <div *ngIf="isVisible">Active</div> <div [hidden]="!isVisible">Active</div>
ngIf
是另一个示例的简写。 当Angular处理该指令时,它会在DOM中写入一个带有隐藏属性的div元素。 ngIf
指令不会在表达式为false时在DOM中呈现div。 使用hidden属性会隐藏浏览器视口中的div内容,但div仍然存在于DOM中。 ngIf
有效,但使用hidden属性是错误的,并将引发错误。 <form #userForm="ngForm"> <input type="text" ngModel name="firstName" required /> <input type="text" ngModel name="lastName" required /> <button (click)="submit(userForm.value)">Save</button> </form>
%OPTION% A
<button (click)="submit(userForm.value)" disable="userForm.invalid">Save</button>
%OPTION% B
<button (click)="submit(userForm.value)" [disabled]="userForm.invalid">Save</button>
%OPTION% C
<button (click)="submit(userForm.value)" [ngForm.disabled]="userForm.valid">Save</button>
%OPTION% D
<button (click)="submit(userForm.value)" *ngIf="userForm.valid">Save</button>
@Component({
selector: 'app-title-card',
template: '<h1 title="User Data"> {{titleText}}</h1>',
})
export class TitleCardComponent {
titleText = 'User Data';
}
<h1 data-title="titleText">{{ titleText }}</h1>
<h1 title="titleText">{{ titleText }}</h1>
<h1 [title]="titleText">{{ titleText }}</h1>
<h1 titleText>{{ titleText }}</h1>
<span>Boss: {{job?.bossName}} </span>
{ path: 'user/:id', component: UserDetailComponent }
{ url: 'user/:id', routedComponent: UserDetailComponent }
{ routedPath: 'user/:id', component: UserDetailComponent }
{ destination: new UserDetailComponent(), route: 'user/:id' }