2016-09-16 152 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

試試這個ANS:http://stackoverflow.com/questions/24761281/hiding-an-option-in-ng - 選項 –

回答

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

爲什麼要刪除從下拉式選單,選擇的選項,你可以很容易地禁止用戶選擇值(有沒有什麼特別的原因?),我會保持它在下拉列表但選項將被禁用。

標記

<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

@Chinu只是其他答案已經做了!我只是建議我覺得有效率.. –

相關問題