2017-06-29 66 views
1

简单脚本有问题。我有一个叫做'FeatureFlag'的模型,它是一个参照父类和子类的树结构。我还有一个叫TreeBuilder的对象,它从FeatureFlag元素列表中生成一棵树。JavaScript推送到数组后,停止循环执行

这里是我的模型:

export class FeatureFlag { 
    private _id: number; 
    private _parent: FeatureFlag; 
    private _children: FeatureFlag[] = []; 

    set id(id:number) { 
     this._id = id 
    } 

    set parent(parent:FeatureFlag) { 
     this._parent = parent 
    } 

    get parent() : FeatureFlag { 
     return this._parent 
    } 

    set children(children:FeatureFlag[]) { 
     this._children = children 
    } 

    get children() : FeatureFlag[] { 
     return this._children 
    }      
} 

和问题的方法,建立()在TreeBuilder作为:

public build() { 
    for(let element of this.data) { //this.data is an array of dict [{node,parent}] 
     let found = this.findNode(element.parent) //searches in tree for node with id 'element.parent' 

     if(found) { //if parent node has been found   
      //found.children.push(element.node) //add current node to its children 
      console.log(found) //print found 
     }   
    } 
} 

当行:

found.children.push(element.node) 

被注释掉,控制台打印所有发现父母。没关系。但是当行被取消注释时,console.log只打印一次 - 第一个找到的父项。为什么会发生?

Regards

+0

控制台没有错误? – Amy

+0

@Amy没有错误:( –

+0

_“这里是我的模型” _你为什么把它叫做一个模型?我只是好奇。 – evolutionxbox

回答

0

Wierd!我会在推送之前放置一个调试器语句,然后逐步完成,以确保它不会抛出一个无声无息的错误。还要确保children.push没有任何意想不到的副作用。

我怀疑children.push是以某种方式投掷,也许是因为孩子没有定义,但Angular正在吞咽这个错误。另外,findNode肯定会返回一个FeatureFlag,而不是别的?

+0

是否有可能我无意中关闭了打印错误?我写了一行应该打印错误但不是 –

+0

请确保您的在Chrome浏览器中确保所有的选项都被选中了,我会尝试......绕过它,然后调试器; console.error(err);开发工具需要为调试器打开声明工作 –

+0

这不是一个像“调试你的代码”语句那样的答案,这可能会更好。 –