ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。上一个值:“未定义”。当前值: ''

时间:2018-06-22 19:57:32

标签: angular5

我正在编写一个带有一个@Input参数并将其显示在html位以下错误的组件。

SuggestionsComponent.html:54 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined'. Current value: '<p>so string value</p>
'.
    at viewDebugError (core.js:9801)
    at expressionChangedAfterItHasBeenCheckedError (core.js:9779)
    at checkBindingNoChanges (core.js:9948)
    at checkNoChangesNodeInline (core.js:14002)
    at checkNoChangesNode (core.js:13976)
    at debugCheckNoChangesNode (core.js:14805)
    at debugCheckDirectivesFn (core.js:14707)
    at Object.eval [as updateDirectives] (SuggestionsComponent.html:54)
    at Object.debugUpdateDirectives [as updateDirectives] (core.js:14689)
    at checkNoChangesView (core.js:13814)

这里是组件。

export class SuggestionsComponent implements OnInit, AfterViewInit {

  @Input() suggestions: Array<Suggestions>;
  @Output() approveSuggestion = new EventEmitter<Object>();

  constructor(
    private elementRef: ElementRef,
  ) {
  }

  ngOnInit() {
  }

  ngAfterViewInit() {
    if (this.suggestions && this.suggestions.length > 0) {
      this.suggestions
        .map((value, index) => {
          this.suggestions[index].newSuggestion = value.descriptionSuggestion;
        });
    }
  }

1 个答案:

答案 0 :(得分:0)

问题在于更改struct ClubMembers { var name: String var active: Bool } func sampleMembers() -> [ClubMembers] { let member1 = ClubMembers(name: "David", active: false) let member2 = ClubMembers(name: "John", active: false) let member3 = ClubMembers(name: "Mitchell", active: true) return [member1, member2, member3] } 中的组件@Input变量值。那时,角度更改视图会改变绑定值,并在不正常的角度更改绑定值。

因此,将ngAfterViewInit()的值移动到@input()可以解决该问题,因为它是在组件自举期间执行的方法。

ngOnInit