简体   繁体   English

* ng创建标签时的角度2 - 表达式在选中后发生了变化

[英]*ngFor angular 2 when creating tabs - Expression has changed after it was checked

I'm using the tab project explained here: Tabs Project 我正在使用这里解释的选项卡项目: Tabs Project

Everything else but my issue works perfectly. 除了我的问题之外的其他所有问 The only thing that doesn't work for me, at the moment, is applying *ngFor when creating tabs. 目前唯一对我不起作用的是在创建标签时应用*ngFor

I know that the 2nd-phase checking by angular detects changes, and he's right, the tabs might be adding while the 2nd-phase check is in progress. 我知道通过角度进行的第二阶段检查会发生变化,而他是对的,在第二阶段检查正在进行时,标签可能会添加。

What I want to do is to try and still make it work, it's super important to me to use *ngFor within the tabs selector. 我想要做的是尽量让它工作,在tabs选择器中使用*ngFor对我来说非常重要。

Provided is a Plunker code demonstrating the crash and what I'm trying to achieve. 提供了一个Plunker代码,展示了崩溃以及我正在努力实现的目标。

Plunker Code Plunker代码

Important to say, Ive looked into 重要的是,我调查了一下

and I understand that its only on debug mode and what the answerer said, though it was a year ago. 我明白它只是在调试模式和回答者所说的,虽然它是在一年前。

In Addition 此外

Unlike the Plunker which able run the code with errors in the console (that's the explanation of the issue), I cant even switch tabs in my project, but that's a normal behavior, I don't want bad code. 与能够在控制台中运行错误代码的Plunker(这是问题的解释)不同,我甚至无法在项目中切换选项卡,但这是正常的行为,我不想要错误的代码。

Unfortunately, I cant share my real code because its for my work basically, but I can provide more data if needed, though it is based almost 100% from the Plunker and the project I provided in the beginning of the issue. 不幸的是,我无法分享我的真实代码,因为它基本上用于我的工作,但是如果需要我可以提供更多数据,尽管它几乎100%来自Plunker和我在问题开始时提供的项目。 .

One solution is to wrap your "zone" code with setTimeout (other methods for triggering change detection manually will also work) 一种解决方案是使用setTimeout包装“区域”代码(手动触发更改检测的其他方法也可以)

if(activeTabs.length === 0) {
      setTimeout(()=>{
        this.selectTab(this.tabs.first);
      },0);
}

Full plunker: https://plnkr.co/edit/UVfiJFYexgua2HfPe0Lw?p=preview 完整的plunker: https ://plnkr.co/edit/UVfiJFYexgua2HfPe0Lw p = preview

In order to fix the issue you need to remove the code for setting the first tab to active from your ngAfterContentInit() method. 为了解决此问题,您需要删除用于将第一个选项卡设置为从ngAfterContentInit()方法激活的代码。 This code is causing the issue: 此代码导致此问题:

if(activeTabs.length === 0) {
  this.selectTab(this.tabs.first);
}

I assume that the error pops up because change detection requires that the DOM is stabilized after one run, and your call in the ngAfterContentInit() would require anothed pass of CD to reflect the new tab.active value in the DOM. 我假设错误弹出,因为更改检测要求DOM在一次运行后稳定,并且您在ngAfterContentInit()中的调用将需要CD的通过传递以反映DOM中的新tab.active值。

What you could do instead is set the first element in your *ngFor to be active by default. 相反,您可以将* ngFor中的第一个元素设置为默认处于活动状态。 Something like: 就像是:

<tab *ngFor="let item of ['1','2']"; let index = index" [active]="index == 0"...

EDIT: Seems you can also use the first local variable (haven't tried it). 编辑:似乎你也可以使用第一个局部变量 (没有尝试过)。 See this plunkr 看到这个plunkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM