2017-09-18 211 views
1

我取得的所有用戶,但我想要的是,他選擇了當前用戶的NG-重複角JS - 由值id選擇選項選擇選項

<select id="taskUser" class="form-control" 
      multiple="multiple" 
      ng-model="taskUser" 
      ng-options="user.ID as user.Name for user in header.users" 
      > 
    </select> 

這是我轉發,我展示瞭如何TE列表是這樣的:

<select id="taskUser" class="form-control ng-pristine ng-untouched ng-valid ng-empty" multiple="multiple" ng-model="taskUser" ng-init=" users = header.users[2].id" ng-options="user.ID as user.Name for user in header.users"> 
    <option label="User 1" value="string:1">User 1</option> 
    <option label="User 2" value="string:2">User 2</option> 
    <option label="User 3" value="string:3">User 3</option> 
</select> 

爲了獲得當前用戶ID,我用{{header.user.ID}}比我得到的只是結果:3

這意味着,要選擇這種需要

<option label="User 3" value="string:3">User 3</option> 

回答

1

$scope.taskUser應列表並定義爲[3],因爲你使用multiple

Fiddle Demo


全碼:

function MyCtrl($scope, $timeout) { 

$scope.header = { 
    users:[ 
    {ID: 1, Name: "AAA"}, 
    {ID: 2, Name: "BBB"}, 
    {ID: 3, Name: "CCC"} 
    ] 
}; 
$scope.taskUser = [3]; 
} 

HTML

<select id="taskUser" class="form-control" 
      multiple="multiple" 
      ng-model="taskUser" 
      ng-options="user.ID as user.Name for user in header.users" 
      > 
    </select> 

    <pre>taskUser: {{taskUser|json}}</pre> 
1

需要在陣列中$scope.taskUser = [3];

var myapp = angular.module('myapp', []); 
 
myapp.controller('FirstCtrl', function ($scope) { 
 
    $scope.header = {}; 
 
    $scope.header.users = [ 
 
     { ID: 1, Name: 'John' }, 
 
     { ID: 2, Name: 'Rocky' }, 
 
     { ID: 3, Name: 'John'}, 
 
     { ID: 4, Name: 'Ben' } 
 
    ]; 
 
    
 
    $scope.taskUser = [3]; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myapp"> 
 
    <fieldset ng-controller="FirstCtrl"> 
 
     <select id="taskUser" class="form-control" 
 
      multiple="multiple" 
 
      ng-model="taskUser" 
 
      ng-options="user.ID as user.Name for user in header.users" 
 
      ></select> 
 
    </fieldset> 
 
</div>

來定義所選擇的值