【发布时间】:2015-12-30 15:53:47
【问题描述】:
我试图从我的 md-select 中删除当前选定的项目。我玩弄了它,但仍然没有得到它。我想我不完全理解 ng-value 在 md-select 中的工作方式。 它不会删除所选项目,还会将所选数组的名称属性添加到列表中的第一位。
html
<div ng-controller="AppCtrl" ng-app="MyApp">
<div layout="column">
<div layout="row">
<span style="font-weight:bold;margin-top: 7px;">remove selected email:</span>
<md-select
data-ng-model="defaultEmailListItem.email"
aria-label="remove_email_md-select"
ng-change="itemChoosen()"
style="margin:0px 5px;width:155px;">
<md-option
ng-repeat="item in emailList "
ng-value="item.name"
ng-bind="item.email">
</md-option>
</md-select>
</div>
<md-button
style="line-height:0px;width:66px;max-width:66px;min-width:66px;"
class="md-raised "
aria-label="remove_email_md_button"
ng-click="removeUserFromEmaillist()"
ga-track-event="['email', 'choose', 'remove']"
>remove
</md-button>
</div>
</div>
js
angular.module('MyApp',['ngMaterial', 'ngMessages'])
.controller('AppCtrl', function($scope) {
$scope.emailList=[
{name:"aaa",email:"aaa@gmail.com"},{name:"bbb",email:"bbb@gmail.com"},{name:"ccc",email:"ccc@gmail.com"},{name:"ddd",email:"ddd@gmail.com"}, {name:"hhh",email:"hhh@gmail.com"}];
$scope.defaultEmailListItem = $scope.emailList[0];
$scope.itemChoosen = function(){
return $scope.defaultEmailListItem;
}
$scope.removeUserFromEmaillist = function(){
$scope.emailList = _.filter($scope.emailList),function(item){
if(item.email !== $scope.defaultEmailListItem.email){
return item ;
}
};
};
});
这是一个密码笔http://codepen.io/anon/pen/VemGzZ?editors=101#0 任何帮助将不胜感激
【问题讨论】:
标签: angularjs angular-material angularjs-ng-change