【问题标题】:Multiple Toggle button default selected values in Angular 7Angular 7中的多个切换按钮默认选择值
【发布时间】:2019-01-16 10:30:10
【问题描述】:

我希望实例化 mat-toggle-button-group 的默认值。出于某种原因,无论何时使用 multiple 属性,值都没有按应有的方式设置。

对于没有多个属性的切换组中的单个默认值,没有问题。我认为它会以同样的方式工作,但显然不是。 我的代码如下所示:

我使用带有输入的表单的主要 html.component

    <app-customer-form [customer]="updateCustomer"></app-customer-form>

表单内部有一个带有与客户相关公司的切换按钮的部分:

    <mat-button-toggle-group  #groupCompany="matButtonToggleGroup" 
             multiple="true"
             [value]="selectedCompanies"
             (change)="onValueChange(groupCompany.value)" 
             >
             <mat-button-toggle *ngFor="let item of companyList" 
         [value]="item">{{item?.name}}</mat-button-toggle>
    </mat-button-toggle-group>

在 ts 文件中,我正在检索对象中的公司并将值存储在数组/对象 selectedCompanies 中。 companyList 被检索并且工作正常。我检查了 console.log 并且 selectedCompanies 像预期的那样被实例化并包含正确的值。但未选择切换按钮。

将 ts.file 放在这里有点困难,因为它确实与其他组件嵌套在一起。但简而言之,它看起来像这样。当点击表格组件中的一行时,它会向父组件发出一个带有客户值的事件,并且在对象内部有公司的属性。发出的值用于使用切换按钮填充表单。

form.ts 文件:

export class FormComponent implements OnInit, OnChanges {
@Input() customer : ICustomer; 
companyList: ICompany[];
selectedCompanies: ICompany[];
constructor( 
    private companyService : CompanyService,
    public sharedService : SharedServices
    ) {}
ngOnInit(){
this.instantiateForm(); // retreives the values for the companyList and 
                        // other default values.
}
ngOnChanges() {
this.setSelectedCompanies();
}
setSelectedCompanies(){
this.selectedCompanies = this.customer.companies
}

有人对此有任何想法吗? 感谢您的帮助!

【问题讨论】:

  • 您可以添加当前出现的屏幕截图吗?
  • @SanyamGoel 是的,我在下一条评论中添加了它。 Tnx 为您提供帮助!
  • 请从答案中删除结果。我能够做到这一点。现在您可以从 ts 文件中发布 selectedCompanies 和 companylist 的代码它们是如何管理的吗?
  • 如果您可以同时发布 HTML 和 ts 文件或 SSCCE (sscce.org),那就太好了。 SSCEE 也是发布问题以更快解决堆栈溢出问题的事实
  • @SanyamGoel 在我的编辑中,我试图向您提供有关我的组件的更多详细信息。我对这一切还很陌生,但我希望这能帮助你理解我想要做什么。

标签: angular toggle default selected


【解决方案1】:

它工作正常。我认为您指的是具有相同属性值的两个不同对象:

查看这个组件的html代码:

<p>
  Default appearance:
  <mat-button-toggle-group name="fontStyle" aria-label="Font Style">
    <mat-button-toggle value="bold">Bold</mat-button-toggle>
    <mat-button-toggle value="italic">Italic</mat-button-toggle>
    <mat-button-toggle value="underline">Underline</mat-button-toggle>
  </mat-button-toggle-group>
</p>

<p>
  Legacy appearance:
  <mat-button-toggle-group appearance="legacy" name="fontStyle" aria-label="Font Style"   multiple="true" [value]="selectedCompanies">

    <mat-button-toggle *ngFor="let company of values" 
         [value]="company">{{company?.name}}</mat-button-toggle>
  </mat-button-toggle-group>
</p>

以及组件的打字稿代码

import {Component} from '@angular/core';

/**
 * @title Button toggle appearance
 */
@Component({
  selector: 'button-toggle-appearance-example',
  templateUrl: 'button-toggle-appearance-example.html',
  styleUrls: ['button-toggle-appearance-example.css'],
})
export class ButtonToggleAppearanceExample {

 values:Comapny[]= [];

 selectedCompanies:Comapny[]=[];

  ngOnInit() {

this.values.push (new Comapny("First Company", "Address 1"));
this.values.push (new Comapny("Second Company", "Address 2"));
this.values.push (new Comapny("Third COmpany", "Address 3"));

this.selectedCompanies.push(this.values[0]);
this.selectedCompanies.push(this.values[1]);

  }

}


class Comapny {

constructor (name:string, address:string){
   this.name=name;
   this.address=address;
  }

  name:string;
  address:string;
}

尝试通过替换 code 在此处运行它

这是带有多属性和两个选定按钮的输出

请确保您在两个列表中引用相同的对象

【讨论】:

  • Tnx,它帮助很大,似乎不同之处在于我将 customer.companies 的值分配给 selectedCompanies 而不是将其推送到 selectedCompanies。这使它起作用:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 2019-08-19
  • 2017-10-02
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 2020-12-04
相关资源
最近更新 更多