多维数组显示angularjs中的值

时间:2014-04-03 11:13:12

标签: javascript angularjs

我想显示我的api中的值,这些值是对象内的对象。这是我认为应该是,它是否正确,我怎么能显示它们?

$scope.tagy = function(){
    for(var i = 0; i < $scope.inventories.length;i++){
        for(var j = 0; j < $scope.inventories[i].tags.length;j++){
            $scope.alltags = $scope.inventories[i].tags[j].name;
        }
    }
    return $scope.alltags;
};

我这样的api

  objects: [
    {
       ....
        something else
       ...
    tags: [
        {
        id: 20,
        name: "secondnewtag",
        resource_uri: "/api/v1/tags/20",
        slug: "secondnewtag"
        },
        {
        id: 21,
        name: "newtag",
        resource_uri: "/api/v1/tags/21",
        slug: "newtag"
        }
     ]
    }
   ]

1 个答案:

答案 0 :(得分:0)

正如Florian所说,要获得所有标签,请使用数组。

$scope.tags = [];
for(var i = 0; i < $scope.inventories.length;i++){
    for(var j = 0; j < $scope.inventories[i].tags.length;j++){
        $scope.tags.push($scope.inventories[i].tags[j].name);
    }
}

http://plnkr.co/edit/mUQ9SgVw3kfszNyUmiRW