ng-repeat& javascript对象

时间:2013-08-25 19:40:47

标签: javascript angularjs

这是我的目标:

     $scope.tickets = [{
         title: "Bug 1",
         number: 1,
         desc: 'Description #1',
         assigned: [{
             name: "Someone",
             group: "Development"
         }]
     },
     {
         title: "Bug 2",
         number: 2,
         desc: 'Description #1',
         assigned: [{
             name: "someone2",
             group: "Development"
         }]
     },
     {
         title: 'Random unknown issue',
         number: 3,
         desc: 'Description #3',
         assigned: [{
             name: "Someone3",
             group: "Support"
         }]
     }];
  1. 我正在做一个表来显示所有内容,并且我正在进行hg-repeat但我不知道如何访问指定的

    下的名称和组信息
    <p class="lead">Development:</p>
    <div class="table-responsive">
      <table class="table table-striped table-condensed">
        <thead>
          <tr>
            <!-- <th>index</th> -->
            <th>#</th>
            <th>Title</th>
            <th>Description</th> 
            <th>Group/Assigned</th>
            <th>Advanced</th>
          </tr>
        </thead>
        <tbody>
          <tr ng-repeat="ticket in tickets">
            <td>{{ticket.number}}</td>
            <td>{{ticket.title}}</td>
            <td>{{ticket.desc}}</td>
            <td>{{ticket.assigned.group}} / {{ticket.assigned.name}}</td>
            <td>
              <button class="btn btn-primary" ng-click="sendToSupport(ticket)">Send to Support</button>
            </td>
          </tr>
        </tbody>
      </table> 
    </div>  
    
  2. 我尝试按

    过滤ng-repeat
     | filter: {group: 'Support'}
    
  3. 并且不起作用

    1. 如您所见,即可将对象发送到ng-click

      <tr ng-repeat="ticket in tickets">
       <td>{{ticket.number}}</td>
       <td>{{ticket.title}}</td>
       <td>{{ticket.desc}}</td>
       <td>{{ticket.assigned.group}} / {{ticket.assigned.name}}</td>
       <td>
        <button class="btn btn-primary" ng-click="sendToSupport(ticket)">Send to Support</button>
       </td>
      

    2. 我正在做的是这个

            $scope.sendToDev = function(ticket){
              this.ticket.assigned.group = "Support"
           }
      

      好吗?

1 个答案:

答案 0 :(得分:2)

您定义为指定为对象数组

assigned: [{
             name: "Someone3",
             group: "Support"
         }]

将其定义为对象(不带[])或将其作为数组访问。

assigned: {
             name: "Someone3",
             group: "Support"
         }

如果你确实需要它成为一个数组,请使用嵌套的ng-repeat,如下所示:

<td><div ng-repeat="assigned in ticket.assigned">{{assigned.group}} / {{assigned.name}}</div></td>