在ui-select列表中显示的重复项,当动态更新新值并更改ui中的选定值时选择多个(角度js)

时间:2015-08-12 04:04:01

标签: angularjs ui-select

无法动态更新新值并更改ui-select中的选定值 更新的值在列表中重复

我添加了让员工更新模型并更新员工以添加新数据并预先选择该值。

我所做的是

  • 创建了一个添加模型按钮,用于填充uiselect内的值。
  • 创建了一个更新模型按钮以添加新值并将其更改为选中
  • 添加模型时,单击它列为item1,item2和item3
  • 点击更新模型后,会添加新项“item4”并将其设为选中

问题是第4项被选中,但它也会显示在选择列表中

下面给出的plunkr链接 http://plnkr.co/edit/RCEQeSp00QhnLl4wegpz?p=preview

 <ui-select style="width: 800px;" ng-disabled="disabled" theme="select2" ng-model="vm.employees.selected" multiple="">
  <ui-select-match placeholder="Select employee...">{{$item.name}} <{{$item.city}}></ui-select-match>
  <ui-select-choices refresh-delay="0" refresh="vm.getdata($select.search)" repeat="employee.id as employee in vm.employees | propsFilter: {name: $select.search}  ">
    <div ng-bind-html="employee.name | highlight: $select.search"></div>
    <small>
    name: {{employee.name}}
    city:           <span ng-bind-html="''+employee.city | highlight: $select.search"></span>
    </small>
  </ui-select-choices>
</ui-select> 

app.controller('DemoCtrl', function($http) {
var vm = this;
vm.name = 'World';

function getemployee() {
    var employees = [];
    $http.get('employees.json').success(function(data) {

        vm.employees = data;
    });
}

vm.updateemployee = function() {
    console.log(vm.employees);

    vm.selvalues = vm.employees.selected;
    vm.employees.selected = undefined;
    vm.employees.push({
        id: 9,
        name: "test",
        city: "chennai",
        state: "TN"
    });
    vm.selvalues.push(9);
    vm.employees.selected = vm.selvalues;
    console.log(vm.employees);
    console.log(vm.employees.selected);
}

vm.getdata = function(query) {
    console.log(vm.employees);

    console.log(vm.employees.selected);

}

vm.employees = {};
vm.selvalues = [];
vm.employees.selected = [];
vm.employees = [];
vm.getemployee = getemployee;});

[plunkr link] [1]

http://plnkr.co/edit/RCEQeSp00QhnLl4wegpz?p=preview

1 个答案:

答案 0 :(得分:1)

    vm.selvalues= vm.employees.selected;
 vm.employees.selected=undefined;
 vm.employees=vm.employees.concat({id:9,name:"test",city:"chennai",state:"TN"});    
 vm.employees.selected = vm.selvalues.concat(9);

改变对concat的推动对我有用。 感谢所有试图帮助我的人

http://plnkr.co/edit/5za5SpGfXx4z2Jy7AocN?p=preview