简体   繁体   English

角4嵌套* ngFor删除父级错误

[英]angular 4 nested *ngFor on delete parent error

I have a problem with nested *ngfor in angular 4, what i need to do is delete parent ngfor i have tried with setting it to null and using delete and neither works so i am kinda out of options. 我在angular 4中嵌套* ngfor有一个问题,我需要做的是删除父ngfor,我尝试将其设置为null并使用delete,但都无法正常工作,所以我有点无法选择。 basically what happens if you delete parent ngFor angular misbehaves(at least i think) and throws error. 基本上,如果删除父ngFor角度异常(至少我认为)并抛出错误,会发生什么。

Can you guys help me out and show me the correct way to do nested *ngfor in which it is possible to remove children, or now in angular 4 it is done on some different way? 你们可以帮我一下,告诉我做嵌套* ngfor的正确方法,以便可以删除子级,或者现在在angular 4中,它是以某种不同的方式完成的?

i have created this test component 我已经创建了这个测试组件

 @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 is the error, can't read much from it other than it tried to read items of null, if i try delete than same error instead of null of undefined, do i somehow need to tell data is changed that part isn't magical any more? 这是错误,除了尝试读取null项以外,无法从中读取太多信息,如果我尝试删除比相同错误而不是undefined为null的错误,我是否需要以某种方式告诉数据更改那部分不是魔术还有吗? If i try this.data.categories[0] = [] or = {} it updates fine the thing is i want to delete data because i want to pass it to server, and because user pressed delete :'( can't believe something like that simply isn't possible? 如果我尝试this.data.categories [0] = []或= {},它可以很好地更新,因为我想将数据传递给服务器,并且因为用户按下了delete,所以我想删除数据:'(不敢相信那样的事情根本不可能吗?

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)

For removing item from Array , you should use Array.splice(index, length) . 要从Array删除项目,应使用Array.splice(index, length)


using delete will make the item tobe undefined , but still there(an item of the Array). 使用delete将使该项目undefined ,但仍然存在(Array的一项)。 same with setting to null . 与设置为null相同。 And for your situation, this will leads to error cannot find something of null/undefined 而对于您的情况,这将导致错误cannot find something of null/undefined

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

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