在Angular 6中重用组件html

时间:2018-05-19 11:30:27

标签: angular angular-cli angular-components angular-cli-v6

我有以下组件模板:

<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中实现这一目标?

2 个答案:

答案 0 :(得分:1)

您可以使用route parameter指定搜索条件。有关演示,请参阅 this stackblitz

  1. search参数添加到组件路径:
  2. { path: "vehicles/:search", component: VehiclesComponent }
    
    1. 为每个路由器链接添加适当的参数:
    2. <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> |
      
      1. 从有效路线中检索搜索条件:
      2. 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();
          }
        }
        
        1. 根据所选搜索条件调整视图,例如使用ngSwitch指令:
        2. <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>
          ...
          

答案 1 :(得分:0)

我会制作一些&#34;模板&#34;。每个模板根据不同的标准都有不同的输入框。让我们说你点击导航&#34; color&#34;然后我会用一个输入框显示该模板,你可以在其中放置颜色名称。然后是Price的另一个模板,你可以像往常一样放置2个输入框,价格低廉。像这样:

<div *ngIf="choosed == 'color'">
    <form class="form-inline my-2 my-lg-0">
        <input class="form-control mr-sm-2" type="text" placeholder="Color" 
         aria-label="Color" [(ngModel)]="color" name="color">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit" 
      (click)="searchVehicle()">Search</button>
    </form>
</div>

<div *ngIf="choosed == 'price'">
    <form class="form-inline my-2 my-lg-0">
        <input class="form-control mr-sm-2" type="text" placeholder="low_price" 
         aria-label="Low Price" [(ngModel)]="low_price" name="low_price">
         <input class="form-control mr-sm-2" type="text" placeholder="high_price" 
         aria-label="High Price" [(ngModel)]="high_price" name="high_price">
        <button class="btn btn-outline-success my-2 my-sm-0" type="submit" 
      (click)="searchVehicle()">Search</button>
    </form>
</div>

然后当你点击导航栏或某个按钮时我会调用一个方法 将选择的变量设置为颜色或价格,模板将根据变量内容显示自己!

希望它有所帮助!