简体   繁体   English

使用Angular材质在Angular中的唯一输入值

[英]Unique input value in Angular using Angular Material

I have the following code in my html, I am trying to let a user input a value in the input field and then get the value in my ts file. 我的html中包含以下代码,我试图让用户在输入字段中输入一个值,然后在我的ts文件中获取该值。 However when I type in one input field, the other input fields below also get populated. 但是,当我输入一个输入字段时,下面的其他输入字段也会被填充。 I need only the input field where the user is typing in to get populated. 我只需要用户在其中输入内容的输入字段即可。 Any thoughts 有什么想法吗

  <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>

I can get the input value successfully in my ts file. 我可以在ts文件中成功获取输入值。

Please see screenshot for visual. 请看屏幕截图。 All input fields are getting '213'. 所有输入字段都为“ 213”。 It must only display in my active input field 它只能显示在我的活动输入字段中 在此处输入图片说明

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;
      });
  }



}

Input in data table 在数据表中输入

Create one more data column like branchCode in table : 在表中再创建一个数据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>

Here you are referencing branchCode to all input. 在这里,您将branchCode引用到所有输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM