Angular - 关于ngFor的ExpressionChangedAfterItHasBeenCheckedError

时间:2018-01-26 16:04:50

标签: angular

我正在用数组中的数据填充表格,我想将自定义输入集中在表格的最后一个元素上,所以我有这个:

        strings = [
            ["aabcc", "adcaa"],
            ["abca", "xyzbac"],
            ["assssbs", "aasaaaa"],
            ["zzzzzz", "zzz"],
            ["abcdefghxyzttw", "hgfedcbaabcwwt"] #<-- the strings failing with one iteration
        ]


        def commonCharacterCount(s1, s2):
            s1, s2 = sorted(list(s1)), sorted(list(s2))
            matches = []

            def matched(i):
                matches.append(i)
                s1.remove(i)
                s2.remove(i)

            [matched(i) for i in s1 if i in s2]
            [matched(i) for i in s2 if i in s1]
            [matched(i) for i in s1 if i in s2] #<-- Second for loop to find f

            return len(matches)


        def test():
            for i in strings:
                commonCharacterCount(i[0], i[1])
            return


        test() 

它按预期工作,但当数组中有多个项目时,我的控制台出错了。我注意到问题出现在这段代码中:

<tr *ngFor="let detail of detailsList; let i = index" (dblclick)="showDetailDetails(detail.ID, i)">
    <th>{{ a few table fields}}</th>
    <th>
        <md-input-container>
            <input [focus]="detailsList.length-1 == i" mdInput type="number" (keypress)="qtKeyDown($event)" (change)="getTotalNetAmount(i)" [(ngModel)]="detail.OrderedQuantity" [ngModelOptions]="{standalone: true}" value="{{detail.OrderedQuantity}}" min="1" OnlyNumber="true" [disabled]="!editMode">
        </md-input-container>
    </th>
    <th>
        (More fields)
    </th>
</tr>

无论如何都要修复此错误?代码正在按预期工作,因此此错误不会打扰网站的行为,问题是我不希望用户在浏览器控制台中看到错误。

1 个答案:

答案 0 :(得分:4)

这通常与changeDetection有关。没有看到你的代码,我猜你在某个时候正在做push()。在push()之后添加:

import { ChangeDetectorRef } from '@angular/core';

constructor(private ref: ChangeDetectorRef) { }


this.something.push(someVar);
this.ref.detectChanges();