如何在AngularJS中删除动态创建的下拉选项?

时间:2016-09-16 12:15:27

标签: angularjs

我发布了我的代码。我想从我的选项中删除Kanhu

参见工作示例。

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}这个不起作用。

4 个答案:

答案 0 :(得分:1)

使用|过滤器:{id:&#39;!&#39; + existingId}

&#13;
&#13;
0
&#13;
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;
 
};
&#13;
&#13;
&#13;

答案 1 :(得分: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>

答案 2 :(得分: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

答案 3 :(得分:0)

您可以尝试使用以下选项:

希望有帮助=)

this.nChartControl
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;
 
};