[英]Unique input value in Angular using Angular Material
我的html中包含以下代碼,我試圖讓用戶在輸入字段中輸入一個值,然后在我的ts文件中獲取該值。 但是,當我輸入一個輸入字段時,下面的其他輸入字段也會被填充。 我只需要用戶在其中輸入內容的輸入字段即可。 有什么想法嗎
<ng-container matColumnDef="branch_code">
<mat-header-cell *matHeaderCellDef mat-sort-header>Enter Branch Code</mat-header-cell>
<mat-cell *matCellDef="let row">
<input matInput placeholder="Enter Branch Code" [(ngModel)]="branchCode">
<mat-icon (click)="gotoWorkstationDetails()" >arrow_forward</mat-icon>
</mat-cell>
</ng-container>
我可以在ts文件中成功獲取輸入值。
請看屏幕截圖。 所有輸入字段都為“ 213”。 它只能顯示在我的活動輸入字段中
import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { MatPaginator, MatTableDataSource, MatSort } from '@angular/material';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Router } from '@angular/router';
@Component({
selector: 'app-workstation',
templateUrl: './workstation.component.html',
styleUrls: ['./workstation.component.scss']
})
export class WorkstationComponent implements OnInit {
title = 'Workstation';
displayedColumns = ['name', 'mac_address', 'address', 'branch_code'];
dataSource = new MatTableDataSource();
@ViewChild(MatSort) sort: MatSort;
@ViewChild(MatPaginator) paginator: MatPaginator;
loading = false;
branchCode;
constructor( private http: HttpClient, public router: Router ) { }
ngOnInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
this.loading = true;
this.http.get<Array<any>>('/api/workstations').subscribe(data => {
this.dataSource.data = data;
this.loading = false;
},
err => {
this.loading = false;
});
}
applyFilter(filterValue: string) {
filterValue = filterValue.trim();
filterValue = filterValue.toLowerCase();
this.dataSource.filter = filterValue;
}
gotoWorkstationDetails() {
console.log(this.branchCode);
this.loading = true;
this.http.get<Array<any>>(`/api/branches/${this.branchCode}/workstations`).subscribe(data => {
const branchDetail = data;
this.router.navigate(['workstation/' + this.branchCode]);
console.log(branchDetail);
this.loading = false;
},
err => {
this.loading = false;
});
}
}
在表中再創建一個數據branchCode
(如branchCode
):
<ng-container matColumnDef="branch_code">
<mat-header-cell *matHeaderCellDef mat-sort-header>Enter Branch Code</mat-header-cell>
<mat-cell *matCellDef="let row">
<input matInput placeholder="Enter Branch Code" [(ngModel)]="row.branchCode">
<mat-icon (click)="gotoWorkstationDetails()">arrow_forward</mat-icon>
</mat-cell>
</ng-container>
在這里,您將branchCode
引用到所有輸入。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.