【问题标题】:Angular 4 checkbox set programmaticallyAngular 4复选框以编程方式设置
【发布时间】:2017-09-01 12:41:56
【问题描述】:

我看到过类似的问题,但我仍然不明白。 我有一个组件女巫获取项目列表Array<{id: number, name: string}> 和“已检查”项目列表Array<number>

@Component({
    selector: 'check-list',
    template: `
    <div class="form-check" *ngFor="let item of list">
        <label class="form-check-label">
            <input class="form-check-input" 
                type="checkbox" 
                [checked]="checked.includes(item.id)"
                (change)="onChange.emit({id: item.id, value: $event.target.checked})">
            {{ item.name }}
        </label>
    </div>
    `
})

export class CheckListComponent {
    @Input() list: CheckListItem[];
    @Input() checked: number[];

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

    constructor() { }
}

如果我更新“选中”列表,渲染的复选框列表也会更新,但如果我单击某些复选框然后更新“选中”列表,它不会按预期呈现。

Plunker:https://plnkr.co/edit/YSItU48QflE4GZbj58Ev?p=preview

单击 plunker 中的两个按钮,它按我预期的方式工作,复选框根据“选中”列表更新。但是如果我点击“item 2”然后点击reset,它不会清除“item 2”。

我也尝试过使用[ngModel][attr.checked]。 我不想使用[(ngModel)] 的原因是我还有一个@Output() 触发服务器请求,然后如果失败,复选框应该更新与否。我使用ngrx 表示状态。

更新:

所以我的问题不够清楚,我正在使用 Redux/ngrx,发送到我的 check-list-component 的列表不应该由组件本身直接改变。我已更新组件以在更改时发回输出

这是新的 plnker:https://plnkr.co/edit/lMouWapI2lb9U6mS9YMy?p=preview

更新 2:

所以我遇到的问题是单击/检查会将更新发送到服务器,如果失败,我希望复选框重置为以前的状态。 如果服务器请求成功与否,我添加了一个随机布尔值。 也许我为此使用了错误类型的实现 ide。

Plunker:https://plnkr.co/edit/nGyS8oYWVzzRULgzcDQV?p=preview

【问题讨论】:

  • 以下答案是否解决了您的问题?
  • 嗯..您的解决方案适用于我发布的案例。我添加了一个新的 plunker
  • 也许,您最初应该添加详细信息,或者在关闭此问题后添加了新问题。检查这个How do I ask a good question?,干杯!!

标签: javascript angular ngrx


【解决方案1】:

当检查更改时,您还需要更新 check-list 组件中的检查数组,

检查清单

@Component({
    selector: 'check-list',
    template: `
    <div class="form-check" *ngFor="let item of list">
        <label class="form-check-label">
            <input class="form-check-input" 
                type="checkbox" 
                [checked]="checked.includes(item.id)"
                (change)="onChange.emit({id: item.id, value: $event.target.checked})">
            {{ item.name }}
        </label>
    </div>
    `
})    
export class CheckListComponent {
    @Input() list: CheckListItem[];
    @Input() checked: number[];
    
    @Output() onChange: EventEmitter<any> = new EventEmitter();

    constructor() { }
}

应用

@Component({
   selector: 'my-app',
   template: `
    <div>
      <h2>Hello {{name}}</h2>
      <check-list [list]="list" [checked]="checked" 
        (onChange)="updateChecked($event)"></check-list>
      <button (click)="setFirstChecked()">Set First Checked</button>
      <button (click)="resetChecked()">Reset Checked</button>
    </div>
   `
})
export class App {      
  ....
  updateChecked($event) {
    // This will be handled in Redux/ngrx...
    if ($event.value) {
      this.checked.push($event.id);
    } 
    else {
      this.checked = this.checked.filter((i) => i !== $event.id);
    }
  }
  ...
}

更新了Plunker!!

【讨论】:

  • 不错的动态方式:)
  • @Swoox,谢谢.. :)
  • 想知道是否放置更多按钮,如果它会起作用。但它做得非常好。
  • 嗨,我会更新我的问题,但是我发送到我的清单的状态是 Redux 不可变状态,因此组件不应自行更新,而是发回事件
  • @Korven,更新了答案和Plunker,也只更新了你的代码,在你的 Plunker 中你正在检查$event.checked,你应该检查$event.value,因为发射的对象有value。干杯!!
猜你喜欢
  • 2018-01-23
  • 2017-09-20
  • 1970-01-01
  • 1970-01-01
  • 2023-03-04
  • 2018-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多