angular - 在 Angular 6 中重用组件 html
问题描述
我有以下组件模板:
<h1>Complexus Vehicle Inventory</h1>
<p *ngIf="!vehicles"><em>No vehicle entries found</em></p>
<div *ngIf="vehicles">
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" [(ngModel)]="strMakeOrModel" name="search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit" (click)="searchVehicle()">Search</button>
</form>
</div>
<table class="table" *ngIf="vehicles">
<thead class="thead-dark">
<tr>
<th scope="col">Make</th>
<th scope="col">Model</th>
<th scope="col">Engine Capacity</th>
<th scope="col">Cylinder Variant</th>
<th scope="col">Top Speed</th>
<th scope="col">Price (R)</th>
<th scope="col">Cylinder Capacity</th>
<th scope="col">Air Pressure/second</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let vehicle of vehicles">
<td>{{ vehicle.Make }}</td>
<td>{{ vehicle.Model }}</td>
<td>{{ vehicle.EngineCapacity }}</td>
<td>{{ vehicle.CylinderVariant }}</td>
<td>{{ vehicle.TopSpeed }}</td>
<td>{{ vehicle.Price }}</td>
<td>{{ vehicle.IndividualCylinderCapacity }}</td>
<td>{{ vehicle.AirPressurePerSecond }}</td>
</tr>
</tbody>
</table>
我希望能够根据在导航栏中单击的导航链接更改表单中的搜索条件。换句话说,假设有人点击了按价格搜索,上面的组件现在应该更新为包含两个文本框,服务于他们想要搜索的价格范围。
表格布局将保持不变,因此这是组件的可重用部分。
你如何在 Angular 6 中实现这一点?
解决方案
您可以使用路由参数指定搜索条件。请参阅此 stackblitz以获取演示。
search
向组件路由添加参数:
{ path: "vehicles/:search", component: VehiclesComponent }
- 为每个路由器链路添加适当的参数:
<a routerLinkActive="active" routerLink="/vehicles/make">Search: make</a> |
<a routerLinkActive="active" routerLink="/vehicles/model">Search: model</a> |
<a routerLinkActive="active" routerLink="/vehicles/price">Search: price</a> |
- 从活动路由中检索搜索条件:
import { ActivatedRoute } from '@angular/router';
import { Subscription } from "rxjs";
...
export class VehiclesComponent {
search: number;
private subscription: Subscription;
constructor(private route: ActivatedRoute) { }
ngOnInit() {
this.subscription = this.route.params.subscribe(params => {
this.search= params["search"];
});
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
- 使视图适应选定的搜索条件,例如使用
ngSwitch
指令:
<form>
...
<ng-container [ngSwitch]="search">
<div *ngSwitchCase="'make'">
<div><input type="radio" name="make">Make 1</div>
<div><input type="radio" name="make">Make 2</div>
<div><input type="radio" name="make">Make 3</div>
</div>
<div *ngSwitchCase="'model'">
<select>
<option>Model 1</option>
<option>Model 2</option>
<option>Model 3</option>
</select>
</div>
<div *ngSwitchCase="'price'">
From: <input type="text">
To: <input type="text">
</div>
</ng-container>
<button>Search</button>
</form>
...
推荐阅读
- python - pd.IndexSlice 列表
- python - Python:如何使 map() 跳过无法转换的数据类型?
- python - MemoryError:无法为形状(53940、53940)和数据类型 float64 的数组分配 21.7 GiB
- testing - grails 4单元测试控制器找不到类
- java - ObjectInputStream readObject 仅在某些 android 设备上返回 null
- javascript - 如何修复 React(TypeScript) 错误“无效的钩子调用。”?
- reactjs - 在 Material UI 中,如何动态地将颜色传递给我的样式?
- azure - Azure Devops 未列出规模集代理池
- git - Git推送错误:RPC失败;结果 = 22,HTTP 代码 = 502
- python - 从键列表中获取dict中的第一个值