Aframe删除for循环中的组件

时间:2017-02-16 11:04:25

标签: aframe

我有一个类似于以下代码的HTML模板

<a-entity id="id1">
    <a-entity template="src: t1.template;type:nunjucks">
    </a-entity>
</a-entity>

t1.template
<a-entity id="id2">
    {% for i in 4 %}
       <a-entity template="src: t2.template; type: nunjucks"></a-entity>
    {% endfor %}
</a-entity>

t2.template
<a-entity id="id3" myComponent="x:4">
    <a-entity>... 
        <a-entity>...</a-entity>
    </a-entity> 
<a-entity>

实体组件根据需要显示在屏幕上。我现在想要在用户点击id3中的任何4个组件时删除整个id1。我的组件代码如下

AFRAME.registerComponent('myComponent', {
    schema: {
        x: {type: 'number', default: 0}
    },
    update: function () {
        //set some attribute for entities inside id3
        //adding event listener to id3. 
        this.el.addEventListener('click', function () {
            setTimeout(function () {
                    var categoryEl = scene.querySelectorAll('#id3');
                    totalCategory = categoryEl.length;
                    for(i=0;i<totalCategory;i++){
                       categoryEl[i].removeAttribute('myComponent');
                       removeAttributeCount++;
                     }              
            }, 1500);
        });
    },
    remove: function () {
        //To check whether component is removed from all element
       if(removeAttributeCount == totalCategory){
             var id1 = this.el.sceneEl.querySelector('#id1');
             id1.parentNode.removeChild(id1);
       }            
    }
}); 

我收到错误

 Uncaught TypeError: Cannot convert undefined or null to object
    at NewComponent.remove (https://cdn.rawgit.com/donmccurdy/aframe-extras/v3.2.7/dist/aframe-extras.js:4742:16)
    at HTMLElement.value (https://aframe.io/releases/0.5.0/aframe.js:71889:17)
    at bound (https://aframe.io/releases/0.5.0/aframe.js:76993:17)
    at Array.forEach (native)
    at HTMLElement.value (https://aframe.io/releases/0.5.0/aframe.js:71567:36)
    at NewComponent.remove (http://localhost:63342/myProj1HTML/Shop/ShopTrail-1/index.js:91:32)
    at HTMLElement.value (https://aframe.io/releases/0.5.0/aframe.js:71889:17)
    at HTMLElement.value (https://aframe.io/releases/0.5.0/aframe.js:71970:16)
    at HTMLElement.value (https://aframe.io/releases/0.5.0/aframe.js:72095:14)
    at HTMLElement.value (https://aframe.io/releases/0.5.0/aframe.js:72015:14)

完美捕捉所有元素。但是从父节点中删除子节点时会发生错误。 有人请帮助让这个工作。提前致谢

0 个答案:

没有答案