将[(ngModel)]添加到单选按钮组后,默认[已选中]不再有效

时间:2017-02-24 16:41:20

标签: angular angular2-forms

我正在开发一个可重复使用的小型组件,它可以设置单选按钮的样式并发出所选的值。

import { Component, OnInit, Input, Output, EventEmitter } from "@angular/core";

@Component({
    moduleId: module.id,
    selector: 'button-select',
    template: `<div class="toggle-group">
                    <div *ngFor="let choice of choices">
                        <input type="radio"
                               id="{{ groupName + choice }}"
                               name="{{groupName}}"
                               value="{{ choice }}"
                               [checked]="choice === defaultChoice"
                               [(ngModel)]="value"
                               (ngModelChange)="choose($event)" />
                        <label class="toggle-button"
                               for="{{ groupName + choice }}">{{ choice }}</label>
                    </div>
                </div>`,
    styleUrls: [
        'editableField.css',
        'buttonSelect.css'
    ]
})

export class ButtonSelectComponent implements OnInit {
    @Input() choices: string[];
    @Input() defaultChoice: string;
    @Input() groupName: string;
    @Input() value: string;

    @Output() valueChosen: EventEmitter<any> = new EventEmitter();

    ngOnInit() {
        this.choose(this.defaultChoice);
    }

    private choose(value: string) {
        this.valueChosen.emit(value);
    }
}

组件的实现方式如下:

<button-select #statusFilter
               [choices]="['All', 'Active', 'Draft']"
               [defaultChoice]="'All'"
               [groupName]="'statusFilter'"
               (valueChosen)="filterChosen('statusFilter', $event)"
</button-select>

向按钮选择组件添加[(ngModel)]="value" (ngModelChange)="choose($event)"之前,[checked]="choice === defaultChoice"指令正确设置了相关checked上的<input />属性。< / p>

添加[(ngModel)]后, ng-reflect-checked="true"被设置,这会阻止视觉样式显示默认值(因为我的CSS使用伪选择器。)

更改[(ngModel)]的{​​{1}}无效。

为什么会发生这种情况,我该如何解决?

3 个答案:

答案 0 :(得分:24)

我想,你不需要这个[checked]="choice === defaultChoice"。试试这个:

<input type="radio"
       id="{{ groupName + choice }}"
       name="{{groupName}}"
       [value]="choice"
       [(ngModel)]="defaultChoice"
       (ngModelChange)="choose($event)" />

选择[value] = [(ngModel)]广播时。

答案 1 :(得分:1)

我能够通过将输入模板更改为:

来发出值并保留默认样式,只需最少的更改
<input type="radio"
       id="{{ groupName + choice }}"
       name="{{groupName}}"
       value="{{ choice }}"
       [checked]="choice === defaultChoice"
       (click)="choose($event['target']['value'])" />

...我发现有点hacky。它也没有解释为什么添加数据/属性绑定会破坏它,所以我愿意接受更多建议。

答案 2 :(得分:0)

export class ConfirmationmodalComponent implements OnInit {
  client_notification: any = false;
  candidate_notification: any = false;
  cancel_associated_session: any = false;

  constructor(
  ) {}

  ngOnInit(): void {
  }
}
<div class="form-check form-check-inline">
  <input
    class="form-check-input"
    type="radio"
    id="inlineRadio1"
    name="cancel_associated_session"
    [(ngModel)]="cancel_associated_session"
    [value]="true"
  />
  <label class="form-check-label" for="inlineRadio1">
    Yes
  </label>
</div>
<div class="form-check form-check-inline">
  <input
    class="form-check-input"
    type="radio"
    id="inlineRadio2"
    name="cancel_associated_session"
    [(ngModel)]="cancel_associated_session"
    [value]="false"
  />
  <label class="form-check-label" for="inlineRadio2">
    No
  </label>
</div>