ng-repeat-start中的Ng重复

时间:2016-10-10 12:09:40

标签: javascript angularjs


我的数据:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // first row should display a banner:
        if indexPath.row == MyRows.banner.rawValue {
            let bannerCell = tableView.dequeueReusableCell(withIdentifier: "BannerTableViewCell") as! BannerTableViewCell

            // ...

            return bannerCell
        }

        // second row should display categories
        if indexPath.row == MyRows.categories.rawValue {
            let categoriesCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! CategoriesTableViewCell

            // ...

            return categoriesCell
        }

        // the other cells should contains title and subtitle:
        let defaultCell = tableView.dequeueReusableCell(withIdentifier: "CategoriesTableViewCell") as! TileAndSubtitleTableViewCell

        // ...

        return defaultCell
    }

我的代码:

$scope.members = [
 {
    projects: [...]
 },
 {
    projects: [...]
 }, ...
] // $scope.members.length == 15

未生成任何元素。我查看了Chrome开发者工具中的Elements,我在<tr> <th ng-repeat-start="member in members" ng-repeat="project in member.projects" ng-repeat-end ng-if="member.projects" > {{project.name}} </th > </tr> 标记中发现了这些评论:

<tr>

我也试过这个:

<!-- ngRepeat: member in members -->

<!-- ngRepeat: member in members -->
<!-- end ngRepeat: member in members --> (x30)

但是我有错误:<tr ng-repeat-start="member in members"> <th ng-repeat="project in member.projects" ng-repeat-end ng-if="member.projects" > {{project.name}} </th > </tr>

有没有人有想法?提前谢谢。

3 个答案:

答案 0 :(得分:2)

更新回答:

只将'projects'数组放入一个新数组中,如下所示:

$scope.projectsOnly = [];//empty array
$scope.members.forEach(function(item){
        $scope.projectsOnly.push(item.projects);
})

然后只循环到该数组中,如下所示:

<tr>
    <th ng-repeat="project in projectsOnly " >
        {{project.name}}
    </th >
</tr>

希望有所帮助,祝你好运。

答案 1 :(得分:0)

您应该使用嵌套的ng-repeat

执行此操作
<tr ng-repeat="member in members">
    <th ng-repeat="project in member.projects" ng-if="member.projects">
        {{project.get('name')}}
    </th>
</tr>

答案 2 :(得分:0)

您不需要ng-if,并且您不应该将ng-repeat开始/结束分开。只需使用两个ng-repeats,一个在另一个内:

<div ng-app="app">
    <table ng-controller="TestController">
        <tr ng-repeat="member in members">
      <th ng-repeat="project in member.projects">
        {{project.name}}
      </th>
    </tr>
  </table>
</div>

,您的控制器可能如下所示:

var app = angular.module('app', []);

app.controller('TestController', function($scope) {
  $scope.members = [{
    projects: [{
      name: '1'
    }, {
      name: '2'
    }]
  }, {
    projects: [{
      name: '3'
    }, {
      name: '4'
    }]
  }];
});