2017-04-26 10 views
2

데이터베이스에 user을 추가하기 위해 양식을 만들려고하지만이 양식에서는 이미 존재하는 모든 역할을 가진 "역할"을 선택하려고합니다 데이터베이스에. 그러나 역할을 선택하고 양식을 게시 할 때 json에서 속성은 새 사용자를 배치하려는 동일한 역할을 가진 데이터베이스에 이미 존재하는 개체의 값을 사용합니다. 어떻게하면 " 텍스트 "전체 개체 대신?문자열 값 대신 객체 가져 오기 선택

<select ng-model="formData.role" 
     ng-options="user as user.role for user in users"> 
</select> 

답변

0

ng-options="u.role as u.role for u in users" 트릭을 할 것입니다.

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

 
function MyCtrl($scope) { 
 
    $scope.users = [{firstname: 'Jon', role : 'Admin'}, 
 
        {firstname: 'Rob', role : 'User'}, 
 
        {firstname: 'Ned', role : 'Guest'}]; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 

 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
    Roles: <select ng-model="selected" ng-options="u.role as u.role for u in users"></select> 
 
    selected: {{selected}} 
 
</div>

0

나는이

<select ng-model="formData.role" 
    ng-options="user.role as user.role for user in users"> 
</select> 

같은 것을 또한 당신이 참조 할 수 있습니다 생각 docs

+0

덕분에, 내가 바보 야 하하 : 여기 데모입니다 – Tewan

0

에하면 user.role 다음은 텍스트하지 전체 사용자 수 있어야한다고 가정 :

<select ng-model="formData.role" 
     ng-options="user.role as user.role for user in users"> 
</select>