首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为在角度2的垫表中实现的垫选择分配所需的值

为在角度2的垫表中实现的垫选择分配所需的值
EN

Stack Overflow用户
提问于 2018-03-02 12:47:24
回答 1查看 9.1K关注 0票数 1

我创建了一个带有两个列acc_idacc_desc的角2的简单垫表。

我使用accountdetails.json从放置在资产文件夹中的accountdetails.service.ts.文件访问这些列的值。

我在帐户描述列中进行了下拉,列出了acc_desc的值。

下面是我的输出

但是在我的表中,我想根据json文件中的acc_desc值初始化它的acc_id值,并在下拉列表中列出,这样,如果需要的话,我可以将它更改为其他选项。

下面是我的代码

account.component.html

代码语言:javascript
复制
<mat-toolbar color="primary" style="width:100%"> WELCOME </mat-toolbar><br/>

<!-- Table starts here -->

<mat-card>
<div class="example-container mat-elevation-z8">

  <mat-table #table [dataSource]="dataSource1">

    <!-- Account No. Column -->
    <ng-container matColumnDef="acc_id">
      <mat-header-cell *matHeaderCellDef> Account ID. </mat-header-cell>
      <mat-cell *matCellDef="let element">{{element.acc_id}}</mat-cell>
    </ng-container>

      <!-- Account Description Column -->
    <ng-container matColumnDef="acc_desc">
      <mat-header-cell *matHeaderCellDef> Account Description </mat-header-cell>
      <mat-cell *matCellDef="let element">
          <mat-form-field >
            <mat-select style="min-width: 200px;" placeholder="" >
              <mat-option *ngFor="let dep of acc_desc " [value]="acc_desc" >
                {{ dep.acc_desc }}
              </mat-option>
            </mat-select>
          </mat-form-field>
      </mat-cell>
       </ng-container>


    <mat-header-row *matHeaderRowDef="displayedColumns1" ></mat-header-row>
    <mat-row *matRowDef="let row; columns: displayedColumns1;"> </mat-row>
  </mat-table>

  <mat-paginator #paginator
                 [pageSize]="10"
                 [pageSizeOptions]="[5, 10, 20]">
  </mat-paginator>
</div>
</mat-card>

account.component.ts

代码语言:javascript
复制
import {Component, ViewChild, Inject, OnInit} from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import {MatPaginator, MatTableDataSource} from '@angular/material';
import { AccountdetailService } from '../accountdetail.service';

@Component({
  selector: 'app-account',
  templateUrl: './account.component.html',
  styleUrls: ['./account.component.scss']
   })


export class AccountComponent implements OnInit {

acc_desc: any;

constructor(private accdetailservice: AccountdetailService ) { }


  /* Table Starts here
  ---------------------- */

 displayedColumns1 = ['acc_id', 'acc_desc'];
 dataSource1= new MatTableDataSource<Element>(ELEMENT_DATA);

ngOnInit(){
  this.accdetailservice.accountdetails()
  .subscribe(data => {
     this.acc_desc = data;
     this.dataSource1.data = data;
  }); }

  @ViewChild(MatPaginator) paginator: MatPaginator;

   ngAfterViewInit() {
    this.dataSource1.paginator = this.paginator;
  } }

  export interface Element {
   acc_id: any;
   acc_desc: any; 
  }

const ELEMENT_DATA: Element[] = [];

accountdetails.service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class AccountdetailService {

  constructor(private http:Http ) { }

  accountdetails()
  {
    return this.http.get('/assets/accountdetails.json')
    .map(result => result.json());
  }}

accountdetails.json

代码语言:javascript
复制
[
    {
        "acc_id": 1001,
        "acc_desc": "Administration"
    },

    {
        "acc_id": 1002,
        "acc_desc": "Laboratory"
    },

    {
        "acc_id": 1003,
        "acc_desc": "Staff"
    },

    {
        "acc_id": 1004,
        "acc_desc": "Office-1"
    },

    {
        "acc_id": 1005,
        "acc_desc": "Office-2"
    }
]

app.module.ts

代码语言:javascript
复制
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ReactiveFormsModule, FormsModule} from '@angular/forms';
import { HttpModule} from '@angular/http';

import { AppMaterialModule } from './app-material.module';


import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { AccountComponent } from './account/account.component';

import { AccountdetailService } from './accountdetail.service';


@NgModule({
  declarations: [
    AppComponent,
    AccountComponent      
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    ReactiveFormsModule,
    BrowserAnimationsModule,
    AppMaterialModule,
    FormsModule ,
    HttpModule   
  ],
  entryComponents:[ ],

  providers: [ AccountdetailService],
  bootstrap: [AppComponent]
})
export class AppModule { }

有谁能告诉我如何用我的json文件中列出的相应acc_id的acc_desc来初始化相应的值,也可以用下拉菜单来选择所需的值。

提前谢谢..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-02 14:27:13

您必须将一个变量绑定到mat-select组件并修复value of mat-option

代码语言:javascript
复制
<mat-select style="min-width: 200px;" placeholder="" [(value)]="element.acc_desc" >
   <mat-option *ngFor="let dep of acc_desc " [value]="dep.acc_desc" >
     {{ dep.acc_desc }}
   </mat-option>
</mat-select>

样本:https://stackblitz.com/edit/angular-isif36?file=app%2Fselect-value-binding-example.ts

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49069432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档