在MatButtonToggleGroup指令

时间:2017-10-20 16:53:38

标签: angular angular-material2

在这个材料2片段中,我们如何确保具有draft值的按钮恰好是默认值? Docs提到selectedchecked指令,但它们似乎不起作用。

<mat-button-toggle-group #group="matButtonToggleGroup" matInput name="status" [(ngModel)]="status" #field_status="ngModel">
    <mat-button-toggle checked? selected? default? value="draft">
    DRAFT <--how do we set this value to be the default selected/checked one? -->
    </mat-button-toggle>
    <mat-button-toggle value="publish">
    PUBLISH
    </mat-button-toggle>
</mat-button-toggle-group>

2 个答案:

答案 0 :(得分:5)

希望这有帮助。

public selectedVal: string;
constructor() { }

ngOnInit(){
  this.selectedVal ='draft';
} 

public onValChange(val: string) {
  this.selectedVal = val;
}

 <mat-button-toggle-group #group="matButtonToggleGroup" [value]="selectedVal" (change)="onValChange(group.value)" >
  <mat-button-toggle value="draft">
    Draft
  </mat-button-toggle>
  <mat-button-toggle value="publish">
    Publish
  </mat-button-toggle>
</mat-button-toggle-group>

答案 1 :(得分:0)

我认为你应该只使用通过status指令绑定的变量ngModel

类似的东西:

ngOnInit() {
    this.status = 'draft';
}

或者只是在声明字段时使用值初始化:

public status = 'draft';