角度过滤下拉模型不更新

时间:2015-04-02 12:59:38

标签: angularjs filter

我尝试在点击按钮时更新过滤下拉列表(组合框)模型。模型值正在更改,但下拉列表仍显示空的选定选项。如果我删除过滤器,一切正常(当然没有过滤)。

Plunker:http://plnkr.co/edit/a48JSEiiATrkcQKxfJjx?p=preview

JS:

$scope.tasks = [
    {name:'Task1', taskid: 1, custid: 2},
    {name:'Task2', taskid: 2, custid: 2},
    {name:'Task3', taskid: 3, custid: 3}
    ];

  $scope.myinfo = {};
  $scope.updateinfo = {name:'Cust Name', custid: 3, taskid: 3};

  $scope.setMyinfo = function(){
    $scope.myinfo = $scope.updateinfo;
  };
});

app.filter('taskFilter', function() {
  return function(input, criteria) {
    var result = [];
    for (var i = 0; i < input.length; i++) {
        if(input[i].custid == criteria || input[i].custid === undefined){
            result.push(input[i]);
        }
    }
    return result;
  }

HTML:

<button ng-click="setMyinfo()">Button</button>
    <br> <br>
    Filtered:
    <select ng-model="myinfo.taskid">
      <option>---</option>
      <option ng-repeat="task in tasks | taskFilter:myinfo.custid" value="{{task.taskid}}">{{task.name}}</option>
    </select>

    Unfiltered:
    <select ng-model="myinfo.taskid">
      <option>---</option>
      <option ng-repeat="task in tasks" value="{{task.taskid}}">{{task.name}}</option>
    </select>

  <br><br>
  Value: {{myinfo.taskid}}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

按键和值预过滤列表:

<div ng-repeat="(k,v) in filterCustId(tasks)">
    {{k}} {{v.pos}}
</div>

在控制器上:

$scope.filterCustId = function(items) {
    var result = {};
    angular.forEach(items, function(value, key) {
        if (!value.hasOwnProperty('custid')) {
            result[key] = value;
        }
    });
    return result;
}

自: https://stackoverflow.com/a/14789258/4668696

答案 1 :(得分:0)

请更新以下代码

if(input[i].custid == criteria || input[i].custid === undefined){

通过

 if(input[i].custid == criteria || input[i].custid === undefined || criteria == undefined){