2017-04-04 77 views
0
<script src="" https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js ""></script> 
<script> 
    var app = angular.module('role', []); 

    app.controller('fooController', function($scope) { 
    $scope.roles = [{ 
     id: 1, 
     name: "Administrator" 
    }, { 
     id: 2, 
     name: "Student" 
    }, { 
     id: 3, 
     name: 'worker' 
    }]; 
    $scope.user = {}; 
    $scope.value=function(){ 
    $scope.user = [name:'abc',roles:[{ 
     id: 2, 
     name: 'Student' 
    }, { 
     id: 3, 
     name: 'worker' 
    }]; 
     } 
    }); 
</script> 

<body> 
    <button type="button" ng-click="value()">Click me </button> 
    <form ng-repeat="test in user"> 
    <input type="text" ng-model="test.name"> 
    <select multiple class="form-control" data-ng-model="user.roles" required> 
    <option ng-repeat="role in roles" value="{{role.id}}">{{role.name}}</option> 
     </select> 
</body> 

我要綁定與NG-模型defalut選擇值我希望只使用NG-重複默認情況下不NG-選擇它sholud在選擇值在$ scope.user.roles值多個默認與NG-模型和NG-重複選擇

+0

什麼是你所面臨的問題? – Sankar

+0

$ scope.user.roles中的值選定爲 –

+0

因此,您希望默認情況下選擇$ scope.user.roles中的值 – Sankar

回答

0

也許這將幫助你:

<select multiple class="form-control" data-ng-model="user.roles" required> 
<option ng-repeat="role in roles" value="{{role.id}}" ng-selected="{{user.roles.indexOf(role) !== -1}}">{{role.name}}</option> 
</select> 
1

這個怎麼樣?

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<script> 
 
    var app = angular.module('role1', []); 
 

 
    app.controller('fooController', function($scope) { 
 
    $scope.roles = [{ 
 
     id: 1, 
 
     name: "Administrator" 
 
    }, { 
 
     id: 2, 
 
     name: "Student" 
 
    }, { 
 
     id: 3, 
 
     name: 'worker' 
 
    }]; 
 
    $scope.user = {}; 
 

 
    $scope.user.roles = [{ 
 
     id: 2, 
 
     name: 'Student' 
 
    }, { 
 
     id: 3, 
 
     name: 'worker' 
 
    }]; 
 
    $scope.user.roles.id = $scope.user.roles.map(function(a) { 
 
     return a.id; 
 
    }); 
 
    }); 
 
</script> 
 

 
<body ng-app="role1" ng-controller="fooController"> 
 
    <select multiple class="form-control" data-ng-value="user.roles" required> 
 
<option ng-repeat="role in roles" ng-selected="user.roles.id.indexOf(role.id) != -1" value="{{role.id}}">{{role.name}}</option> 
 
    </select> 
 
</body>

+0

你能告訴我如何將ng-model分配給輸入文件 –

+0

@banuprakash我建議你使用https://www.npmjs.com/package/ ng-file-upload – Sankar

+0

看到編輯的問題 –