html - 在 select 标签中使用 ngRepeat 两次

标签 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}} 的值{{用户名}}。谁能帮我解决这个问题。我怎样才能在这里获得这些值。

最佳答案

试试这段代码

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>

关于html - 在 select 标签中使用 ngRepeat 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22964977/

相关文章:

包含数百万行的 JavaScript 数据网格

JavaScript html 编辑错误

html - Bootstrap 封面模板 : where to place image?

javascript - Bootstrap 导航栏 #links 正在被 angularjs 更改(或不工作)

javascript - 无法在 IIEF 中传递变量

javascript - 在 Angular 中从路由器访问变量

javascript - Protractor 中的绑定(bind)选择器捕获整个 div 而不是仅捕获值

javascript - 前端js框架可以用于非单页应用程序吗?

javascript - 对使用 $translatePartialLoaderProvider 的 AngularJS 应用程序进行单元测试

javascript - 使用存储在范围变量中的数据而不是进行 ajax 调用