Angular使用textarea抛出ExpressionChangedAfterItHasBeenCheckedError

时间:2017-06-16 19:12:18

标签: angular textarea

当我单击列表下的其中一个项目并转到详细信息页面时,它会抛出ExpressionChangedAfterItHasBeenCheckedError和DebugContext。

我能够将我的原始项目剥离为plunker并重现错误。 只有Textarea才会出现错误。如果我用输入替换Textarea就可以了。

Plunkr Link

The exception

请参阅第3条评论,了解实例的链接和详细信息。

import { Component, Input, OnInit, OnDestroy } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { BeginEventEmitter, EndEventEmitter } from './emitter';
import { SelectSourceService } from "./sourceselect.service";

import { CodeService } from "./codes.service";
import { Code } from "./code"

@Component(
    {
        selector: 'Detail',
        template: `    <h3>Details</h3>
    <form>
      <div class="form-group">
        <label for="id">ID</label>
        <input type="text" class="form-control" id="id" name="id" [(ngModel)]="theCode.id">
      </div>
      <div class="form-group">
        <label for="description">description</label>
        <textarea class="form-control" id="description" name ="description" rows=10 cols=30 [(ngModel)]="theCode.description"></textarea>
      </div>
      <button type="button" class="btn btn-default" (click)="OnFormSubmit()">Submit</button>
    </form>`
    })
export class DetailsComponent implements OnInit, OnDestroy  {

    theCode: Code;
    id: string;
    showLoader: boolean = true;
    source : string;

    setDetail(): void {
        this.theCode = this.codeService.getCode4Id(this.source, this.id);
    }

    constructor(private codeService: CodeService, private route: ActivatedRoute, private router: Router, 
        private _selSourceService: SelectSourceService, private opening:BeginEventEmitter, private closing:EndEventEmitter) {

    }

    ngOnInit()
    {
        this.route.params
            .subscribe(value => this.id = value['id']);
        this.subscription = this._selSourceService.sourceSelection$.subscribe((selection: string) => { this.source = selection; this.setDetail(); });
        this.opening.emit("Opening Details for " + this.id);
    }

    ngOnDestroy() {
        this.closing.emit("Closing Details for " + this.id);
    }

    OnFormSubmit(data: Code): void {
        alert("The Changes were submitted");
        this.router.navigate(['/Codes']);
    }

    OnFormCancel() : void {
        alert("The form was cancelled");
    }
};    

1 个答案:

答案 0 :(得分:1)

打开您的app组件,然后找到ngOnInit挂钩并尝试替换

this.openDetails.subscribe(() => { this.detailOpen++; })

this.openDetails.subscribe(() => Promise.resolve(null).then(() => this.detailOpen++));

<强> Updated Plunker