使用 md-select 和 ng-change 从对象数组中删除项目
Remove item from array of object with md-select and ng-change
我正在尝试从我的 md-select 中删除当前 selected 项目。我玩弄它,但仍然没有得到它。我想我不完全理解 ng-value 在 md-select 中的工作方式。
它不会删除 selected 项,还会将 selected 数组的名称 属性 添加到列表的第一位。
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
任何帮助将不胜感激
已更新codepen
问题:
ng-value="item.email"
而不是 item.name
.
_.filter(Array, callback)
。您没有提供 callback 函数,而是在关闭括号后刚刚声明了一个函数!
我正在尝试从我的 md-select 中删除当前 selected 项目。我玩弄它,但仍然没有得到它。我想我不完全理解 ng-value 在 md-select 中的工作方式。 它不会删除 selected 项,还会将 selected 数组的名称 属性 添加到列表的第一位。
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 任何帮助将不胜感激
已更新codepen
问题:
ng-value="item.email"
而不是item.name
._.filter(Array, callback)
。您没有提供 callback 函数,而是在关闭括号后刚刚声明了一个函数!