2016-09-16 19 views
0

私のコードを掲載しました。私はオプションからKanhuを削除したいと思います。AngularJSで動的に作成されたドロップダウンオプションを削除するにはどうすればよいですか?

実例を参照してください。ここで

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

 
function myCtrl ($scope) { 
 
    $scope.userProfiles = [ 
 
    {id: 10, name: 'Chinmay'}, 
 
    {id: 27, name: 'Sanjib'}, 
 
    {id: 35, name: 'Kanhu'}, 
 
    {id: 38, name: 'Pradeepta'}, 
 
    {id: 39, name: 'Debsish'}, 
 
    ]; 
 
    $scope.selectedUserProfile= $scope.userProfiles[1].id; 
 

 
    $scope.existingId = 35; 
 
    
 
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    Name 
 
    <select id="requestorSite" ng-model="selectedUserProfile" ng-options="userProfile.id as userProfile.name for userProfile in userProfiles"> 
 
    </select> 
 
    <br/> 
 
    </div>

はドロップダウンに来て5名ですが、私は私のドロップダウンオプションからkanhuを削除したいです。それを取り除く方法を教えてもらえますか?

私はフィルタuserProfile.id as userProfile.name for userProfile in userProfiles | filter:{id: !existingId}を使用しようとしましたが、これは動作しません。

+0

してみてください。http://stackoverflow.com/questions/24761281/hiding-an-option-in-ng -options –

答えて

1

フィルタ:{id: '!' + existingId}

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

 
function myCtrl ($scope) { 
 
    $scope.userProfiles = [ 
 
    {id: 10, name: 'Chinmay'}, 
 
    {id: 27, name: 'Sanjib'}, 
 
    {id: 35, name: 'Kanhu'}, 
 
    {id: 38, name: 'Pradeepta'}, 
 
    {id: 39, name: 'Debsish'}, 
 
    ]; 
 
    $scope.selectedUserProfile= $scope.userProfiles[1].id; 
 

 
    $scope.existingId = 35; 
 
    
 
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    Name 
 
    <select id="requestorSite" ng-model="selectedUserProfile" ng-options="userProfile.id as userProfile.name for userProfile in userProfiles | filter: {id: '!' + existingId}"> 
 
    </select> 
 
    <br/> 
 
    </div>

+0

完璧な作業です。ありがとうございました – Chinmay235

0

あなたが代わりにオプションを使用して、次のことを試すことができます。

はそれが=をホープ)

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

 
function myCtrl ($scope) { 
 
    $scope.userProfiles = [ 
 
    {id: 10, name: 'Chinmay'}, 
 
    {id: 27, name: 'Sanjib'}, 
 
    {id: 35, name: 'Kanhu'}, 
 
    {id: 38, name: 'Pradeepta'}, 
 
    {id: 39, name: 'Debsish'}, 
 
    ]; 
 
    $scope.selectedUserProfile= $scope.userProfiles[1].id; 
 

 
    $scope.existingId = 35; 
 
    
 
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="myCtrl"> 
 
    Name 
 
     <select id="requestorSite" ng-model="selectedUserProfile"> 
 
    <option ng-hide="userProfile.id == existingId" value="{{userProfile.id}}" ng-repeat="userProfile in userProfiles">{{userProfile.name}}</option> 
 
    </select> 
 
    <br/> 
 
    </div>

1

は、ドロップダウンからその選択したオプションを削除したいなぜ、あなたは簡単に(具体的な理由がある?)、その値を選択するようにユーザーを禁止することができますHere is working fiddle

<div ng-app="myApp" ng-controller="myCtrl"> 
Name 
    <select id="requestorSite" ng-model="selectedUserProfile" ng-options="userProfile.id as userProfile.name for userProfile in userProfiles | filter:'!Kanhu'"> 
</select> 
<br/> 
</div> 
1

、これを試してみてください、私はドロップダウンにそれを保つが、そのうオプションが無効になります。

マークアップこれはANSここ

<select id="requestorSite" 
    ng-model="selectedUserProfile" 
    ng-options="userProfile.id as userProfile.name disable when (userProfile.name == 'Kanhu') for userProfile in userProfiles"> 
</select> 

Plunkr in action

同様answer

+0

お返事ありがとうございます。無効にする代わりにその要素を削除する方法は? – Chinmay235

+1

@チュニックちょうど他の答えはすでにやった!私はちょうど私が効率的と感じたものを提案した –

関連する問題