【问题标题】:Angular throws ExpressionChangedAfterItHasBeenCheckedError with textareaAngular 用 textarea 抛出 ExpressionChangedAfterItHasBeenCheckedError
【发布时间】:2017-06-16 19:12:17
【问题描述】:

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

我能够剥离我的原始项目以进行 plunker 并重现错误。 该错误仅发生在 Textarea 中。如果我用 Input 替换 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");
    }
};    

【问题讨论】:

标签: angular textarea


【解决方案1】:

打开你的 app 组件然后找到 ngOnInit 钩子并尝试替换

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

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

Updated Plunker

【讨论】:

  • 谢谢。为什么这行得通?我也对我原来的项目做了同样的改变,但没有帮助。
  • 之所以有效,是因为它在下一次视图检查中执行。你能告诉我它不工作的活生生的例子吗?
  • 这里是部署的test 站点。使用 Test1@a.com 和 Test1@a.com 登录。详细单击其中一项并向下滚动并点击保存。您会注意到 1. 错误 2. 成功警报后无法加载列表。 3. 下拉列表应该在返回列表页面时启用,但它仍然处于禁用状态。
  • 我需要一个最小的例子。我无权访问您的网站This site has been blocked by the network administrator
  • adsportal.myadsc.com/testcode 我刚刚使用此链接adsportal.myadsc.com/testcode 从 chrome 和 IE 登录,并且能够使用上述信息登录并进入实践。你能再试一次,让我知道你在什么时候得到错误。谢谢
猜你喜欢
  • 2018-05-31
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 2017-11-25
  • 2018-01-20
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
相关资源
最近更新 更多