Angular2 Component @Input two way binding

后端 未结 2 1323
攒了一身酷
攒了一身酷 2021-01-30 19:40

I have a data driven Angular application. I have a toggle component which I pass in a toggled state. My issue is that the two way data binding does not seem to work unless i p

2条回答
  •  庸人自扰
    2021-01-30 20:21

    For [(toggled)]="..." to work you need

      @Input() toggled: boolean;
      @Output() toggledChange: EventEmitter = new EventEmitter();
    
      changeValue() {
        this.toggled = !(this.toggled); 
        this.toggledChange.emit(this.toggled);
      }
    

    See also Two-way binding

    [UPDATE] - 25 June 2019
    From @Mitch's comment below:
    It's worth noting that the @Output name must be the same as the @Input name, but with Change on the end. You can't call it onToggle or something. It's a syntax convention.

提交回复
热议问题