双向绑定在Angular 5中无法正常工作

时间:2018-01-11 08:19:10

标签: javascript angular5

有人可以帮助理解我这样做的方式吗?

<app-tests-list
  [(ngModel)]="selectedTests"
  [(indeterminate)]="globalCheckbox.indeterminate"

  [(allSelection)]="globalCheckbox.value"
></app-tests-list>

在子组件中我这样做

  @Input('indeterminate') _indeterminate: boolean;
  @Output('indeterminate') _indeterminateChange = new EventEmitter<any>();
  @Input('allSelection') _allSelection: boolean;
  @Output('allSelection') _allSelectionChange = new EventEmitter<any>();

  setIndeterminate(value) {
    this._indeterminate = value;
    this._indeterminateChange.emit(this._indeterminate);
  }

  setAllSelection(value) {
    this._allSelection = value;
    this._allSelectionChange.emit(this._allSelection);
  }

但是在父组件中,我只能像这样使用它

<app-tests-list
  [(ngModel)]="selectedTests"
  [indeterminate]="globalCheckbox.indeterminate"
  (indeterminate)="globalCheckbox.indeterminate = $event"

  [allSelection]="globalCheckbox.value"
  (allSelection)="globalCheckbox.value = $event"></app-tests-list>

但它并不像我预期的那么美丽:)

0 个答案:

没有答案