【问题标题】:Unique input value in Angular using Angular MaterialAngular 中使用 Angular Material 的唯一输入值
【发布时间】:2018-08-29 08:33:02
【问题描述】:

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



}

【问题讨论】:

    标签: angular typescript angular-material


    【解决方案1】:

    Input in data table

    在表中再创建一个数据列,如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 引用到所有输入。

    【讨论】:

    • 感谢这解决了输入到特定字段的问题,但现在我如何将值输入到我的 ts 文件中。我在控制台中得到一个未定义的。它以前工作过。我正在使用点击事件来获取值
    • 分享你的ts请完成。
    • 使用 this.dataSource.data
    • 转到演示链接查看演示
    • 是的,这行得通,非常感谢。我还发现了一种不太有效的方法,方法是将 row.branchCode 作为点击函数中的事件传递
    猜你喜欢
    • 2021-06-23
    • 2020-01-27
    • 2018-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多