angularjs中ng-repeat的动态模型名称

时间:2015-02-09 14:13:18

标签: django angularjs angularjs-ng-repeat

我正在使用带有django的angularjs。在我的html页面中,我正在执行以下操作:

<div ng-repeat="loc in locations" ng-init="locIndex = $index" ng-show="tab===1">
   Id: {[{loc[0]}]}
   Location Name: {[{loc[1]}]
</div>

现在,我感兴趣的是:

<div ng-repeat="loc in locations" ng-show="tab===1">
   Id: {[{loc[0]}]}
   Location Name: {[{loc[1]}]
</div>
<div ng-repeat="cloc in childLocation{[{loc[0]}]}">
   Child Id:{[{loc[0]}]}
   Child Location Name: {[{loc[1]}]
</div>

在app.js中,我会在用户点击父位置时初始化子位置

$scope.getChildLocations=function(locationId){
        $http({
            method: "get",
                url: '/get_child_locations?locationId='+locationId,
            }).success(function(data, status) {
                  childLocation='childLocation'+location
                  $scope.childLocation=data;
            });
    }

是否可以这样做。如果是的话,我怎么会收到这个错误..

1 个答案:

答案 0 :(得分:0)

您收到错误是因为loc未在您的第二个ng-repeat中定义,因为它未嵌套。

尝试:

<div ng-repeat="loc in locations" ng-show="tab===1">
    Id: {[{loc[0]}]}
    Location Name: {[{loc[1]}]
    <div ng-repeat="cloc in childLocation{[{loc[0]}]}">
        Child Id:{[{loc[0]}]}
        Child Location Name: {[{loc[1]}]
    </div>
</div>