2017-04-09 56 views
-1

我有一个嵌套的* ngfor在角度4的问题,我需要做的是删除父ngfor我已经尝试将其设置为空和使用删除,既不工作,所以我是没有选择。基本上会发生什么,如果你删除父ngForFor错误行为(至少我认为)并抛出错误。角度4嵌套* ngFor上删除父错误

你们可以帮我解决,让我看看正确的方法来做嵌套的* ngfor,在其中可以删除孩子,或者现在在角度4,它是以不同的方式完成的?

我创造了这个测试组件

@Component({ 
 
    selector: 'test-parent', 
 
    templateUrl: './test.component.html', 
 
}) 
 
export class TestComponent { 
 
    constructor() { 
 
     console.log("loaded menu"); 
 
     setTimeout(()=> { 
 
      this.data.categories[1] = null; 
 
     }, 1000); 
 
    }; 
 
    data = { 
 
     categories: [ 
 
      { 
 
       name: 'name 1', 
 
       items: [ 
 
        { 
 
         name: 'item 1' 
 
        }, 
 
        { 
 
         name: 'item 2' 
 
        }, 
 
       ] 
 
      }, 
 
      { 
 
       name: 'name 2', 
 
       items: [ 
 
        { 
 
         name: 'item 3' 
 
        } 
 
       ] 
 
      } 
 
     ] 
 
    } 
 
}
<div *ngFor="let c of data.categories"> 
 
    {{c.name}} 
 
    <div *ngFor="let i of c.items"> 
 
     {{i.name}} 
 
    </div> 
 
</div>

这是错误,无法从它的其他读得多比它试图读取空的项目,如果我尝试删除比同样的错误,而不是空的未定义, 我不知道需要告诉数据改变,该部分是不是神奇更多?如果我尝试this.data.categories [0] = []或= {}它更新正常的事情是我想删除数据,因为我想传递给服务器,并且因为用户按下删除:'(不能相信这样的事情根本是不可能的?

ERROR TypeError: Cannot read property 'items' of null 
at Object.View_TestComponent_1.currVal_0 [as updateDirectives] (TestComponent.html:3) 
at Object.debugUpdateDirectives [as updateDirectives] (core.es5.js:12613) 
at checkAndUpdateView (core.es5.js:12025) 
at callViewAction (core.es5.js:12340) 
at execEmbeddedViewsAction (core.es5.js:12312) 
at checkAndUpdateView (core.es5.js:12026) 
at callViewAction (core.es5.js:12340) 
at execComponentViewsAction (core.es5.js:12286) 
at checkAndUpdateView (core.es5.js:12031) 
at callViewAction (core.es5.js:12340) 
at execEmbeddedViewsAction (core.es5.js:12312) 
at checkAndUpdateView (core.es5.js:12026) 
at callViewAction (core.es5.js:12340) 
at execComponentViewsAction (core.es5.js:12286) 
at checkAndUpdateView (core.es5.js:12031) 
+0

为什么不拼接数组?有没有理由不起作用? –

+0

我已经尝试过它与这个测试场景一起工作,但是tbh我喜欢4个嵌套for循环槽不同组件,altought它应该工作我得到这个相同的错误 –

回答

2

对于从Array删除的项目,你应该使用Array.splice(index, length)


使用delete将使项目砥undefined,但仍然存在(的项目阵列),设置为null。 对于你的情况,这个无线会导致错误cannot find something of null/undefined

+0

非常感谢你的工作,我以为我已经尝试过它, t工作,反正不删除完全删除数组成员? –

+0

@VjeranMagisterLudi还有问题吗? – Pengyy

+0

nop它是固定的,我现在正在做一些广泛的测试 –