检查后表达式已更改。上一个值:“ null:0”。当前值:“ null:4”

时间:2019-09-03 09:29:23

标签: angular typescript

我收到以下错误:

“检查后表达式已更改。上一个值:'null:0'。当前值:'null:4'。”

当我移动“ this.ondetails.emit(this.items.length);”行时对于构造函数,它没有为我提供初始值,因此我不得不将其移至ngOnInit()。

  ngOnInit() {
    this.ondetails.emit(this.items.length);
  }

这给了我所需的初始值,应用程序运行良好,但是当我这样做时,我收到了错误消息。

notifications-widget.component.ts

export class NotificationsWidgetComponent implements OnInit, AfterViewInit {
  @Input() config: NotificationsConfigComponent;
  @Output() ondetails = new EventEmitter();

  public items = [
    {
      title: 'Import of .......... failed',
      description:
        'Lorem ipsum dolor sit amet',
      read: true
    },
    {
      title: 'Manager ..........approved the budget',
      description:
        'Lorem ipsum dolor sit amet',
      read: true
    }
  ];

  constructor() {
  }

  deleteWidget(i) {
    this.items.splice(i, 1);
    this.ondetails.emit(this.items.length);
  }
  itemRead(i) {
    if (this.items[i].read == false) {
      this.items[i].read = true;
    }
  }
  ngOnInit() {
    this.ondetails.emit(this.items.length);
  }

  ngAfterViewInit() {
  }
}

layout.component.html

<gridster [options]="options">
    <gridster-item *ngFor="let item of layout"
          <notifications-widget *ngSwitchCase="'Notifications'"

              (ondetails)="item.widgetDetails = $event">

          </notifications-widget>                   
    </gridster-item>
</gridster>

widget-creator.component.ts

export class WidgetCreatorComponent implements OnInit {
    @Output() onsave = new EventEmitter<any>(null);
    widgetType = WidgetType.KPI;
    widgetTypes = WidgetTypes;

    constructor(private layoutService: LayoutService) { }

    save() {
        let item: GridsterItem = {
            widgetType: this.widgetType,
            widgetConfig: this.setWidgetConfig(this.widgetType),

            widgetDetails: null,

        };
        this.layoutService.addItem(item);
        this.onsave.emit(this.widgetType);
    }
}

1 个答案:

答案 0 :(得分:2)

更新解决方案:

layout.component.ts

constructor(private cd: ChangeDetectorRef) { }

 ngAfterViewChecked() {
    this.cd.detectChanges();
  }