在angular中是否有任何方法可以创建类似于结构的哈希映射

时间:2015-06-17 05:24:21

标签: angularjs

我有一个名为domain的父级,我们可以在其下添加多个子级,在每个子级下我们可以添加多个子子级。我想创建一个像这样的结构

enter image description here

我想在所选的商业领域下添加一个子项。它适用于第一个元素,但是当我在任何其他Business域下添加一个子子元素时,它会添加所有创建的子代码:

enter image description here

这是我存储子列表的服务方法:

app.factory('DomainNameService',['$q', function($q) {
var childSubDomainName=[];
     setBusSubDomain:function(val,busDomain){//In val I am getting the business 
//domain selected and in busDomain I am getting the name entered in name field
              if(childSubDomainName.length<1){
                childSubDomainName.push(busDomain);
            }
            else  {
                for(var i=0;i<childSubDomainName.length;i++){
                    var index = childSubDomainName[i].name.indexOf(busDomain.name);//Added this for unique name check
                }
                if(index==-1){//If the name is unique then i allow it to add to childSubDomainName
//Here I want to implement a logic that busDomain gets add only to the parent selected but I am not getting how to do this
                        childSubDomainName.push(busDomain);
                }
                else
                 this.error = 'Name already in use';
                }
    },
}

我正在为childSubDomainName

获取这种类型的结构

enter image description here

任何人都可以建议我怎么做。我尝试了很多东西,但没有用。

1 个答案:

答案 0 :(得分:1)

用于在heirarchy中插入对象的循环结构应该包括检查子项需要插入的确切位置。这将涉及两个条件检查:  1.为父母姓名  2.对于父对象下存在的子进程。  3.如果上述两个条件仅匹配,则应将子子项插入相应的父子项层次结构中。

层次结构可以保持在下面的结构中

[
    {
        "parentid": "id",
        "parentName": "name1",
        "children": [
            {
                "childid": "c_id1",
                "childname": "cn1",
                "subchild": [
                    {
                        "sbid": "id",
                        "sbname": "name"
                    },
                    {
                        "sbid": "id",
                        "sbname": "name"
                    }
                ]
            },
            {
                "childid": "c_id2",
                "childname": "cn2",
                "subchild": [
                    {
                        "sbid": "id",
                        "sbname": "name"
                    },
                    {
                        "sbid": "id",
                        "sbname": "name"
                    }
                ]
            }
        ]
    },
    {
        "parentid": "id",
        "parentName": "name1",
        "children": [
            {
                "childid": "c_id1",
                "childname": "cn1",
                "subchild": [
                    {
                        "sbid": "id",
                        "sbname": "name"
                    },
                    {
                        "sbid": "id",
                        "sbname": "name"
                    }
                ]
            },
            {
                "childid": "c_id2",
                "childname": "cn2",
                "subchild": [
                    {
                        "sbid": "id",
                        "sbname": "name"
                    },
                    {
                        "sbid": "id",
                        "sbname": "name"
                    }
                ]
            }
        ]
    } 
]