在select标记内使用两次ngRepeat

时间:2014-04-09 14:01:10

标签: html angularjs

我使用了以下代码:

<div class="col-lg-3">
   <select ng-model="userportal.data.owner_id" class="form-control uniformselect" id="owner_id" style="border-radius: 4px;">
      <option value="" id=""> Select Device </option>
      <option ng-repeat="device in deviceStatus" value="{{device.device_id}}">
           <span ng-repeat="user in userFilter">
                <span ng-if="{{user.id}} == {{device.device_id}}">
                    {{user.name}}&nbsp;{{device.device_id}}
                <span/>
           </span>
      </option>
  </select>
</div>

我得到{{device.device_id}}的值但是,我没有得到{{user.id}}{{user.name}}的值。任何人都可以帮我解决这个问题。我怎样才能在这里得到这些值。

1 个答案:

答案 0 :(得分:1)

试试这段代码

<强> Working Demo

<强>脚本

   var myApp = angular.module('myApp', []);
myApp.controller('Controller', function ($scope) {
    $scope.userFilter = [{
        id: 1,
        name: 'ABC'
    }, {
        id: 2,
        name: 'LMN'
    }, {
        id: 3,
        name: 'OPQ'
    }, {
        id: 4,
        name: 'RST'
    }, {
        id: 5,
        name: 'UVW'
    }];
    $scope.deviceStatus = [{
        device_id: 1
    }, {
        device_id: 4
    }, {
        device_id: 5
    }];

    $scope.result = [];
    angular.forEach($scope.deviceStatus, function (deviceObj) {
        angular.forEach($scope.userFilter, function (userObj) {
            if (deviceObj.device_id == userObj.id) {
                var newObj = {};
                newObj.id = userObj.id;
                newObj.name = userObj.name;
                $scope.result.push(newObj);
            }
        });
    });
});

<强> HTML

<div ng-app="myApp" ng-controller="Controller">
    <div class="col-lg-3">
        <select ng-model="userportal.data.owner_id" class="form-control uniformselect" id="owner_id" style="border-radius: 4px;">
            <option value="" id="">Select Device</option>
            <option ng-repeat="users in result" value="{{users.id}}">{{users.name}}&nbsp;{{users.id}}</option>
        </select>
    </div>
</div>