【问题标题】:Hide items of autocomplete with watch Angularjs使用手表 Angularjs 隐藏自动完成项
【发布时间】:2016-10-28 23:35:18
【问题描述】:

我正在尝试使用 $scope.watch 进行自动完成,它运行良好,但是当我选择一个选项时,在项目中单击时,它会输入输入,但项目不会隐藏,因为当我输入时它在输入中再次激活手表...

当我选择其中一项时,我不知道如何隐藏这些项目。

任何帮助..

<input type="text" ng-model="selected" />
<ul class="listFavoInbox">
    <li ng-repeat="favo in resultAuto" ng-click="itemFavoInbox(favo.first,favo.id)">
        {{favo.first}}
    </li>
</ul>
$scope.selected = "";
$scope.arrayFavo = [
    {'id': 1, 'first': 'John', 'last': 'Depp', 'age':52, 'gender':'male'}, 
    {'id': 2, 'first': 'Sally', 'last': 'JoHanson', 'age':13, 'gender':'female'},
    {'id': 3, 'first': 'Taylor', 'last': 'Swift', 'age':22, 'gender':'female'},
];

$scope.$watch('selected', function() {
    $scope.resultAuto = [];
    for(var i=0; i<$scope.arrayFavo.length; i++){

        var text = angular.lowercase($scope.arrayFavo[i].first);
        var search = angular.lowercase($scope.selected);

        // console.log("text array",text);
        // console.log("text",$scope.selected);

        if ( search != "" ){
            if ( text.indexOf(search) !== -1 ){
                $scope.resultAuto.push($scope.arrayFavo[i]);
            }

            console.log($scope.resultAuto);
        }
    }
});

$scope.itemFavoInbox = function (name,id){
    $scope.selected = name;
    $scope.resultAuto = "";
}

【问题讨论】:

    标签: javascript angularjs watch angularjs-watch


    【解决方案1】:

    你可以使用 ng-hide =true; 当点击 itemFavoInbox(); 当观察者行动时为假

    【讨论】:

      【解决方案2】:

      您不需要$watch()。使用filter

      <input type="text" ng-model="selected" />
      <ul class="listFavoInbox">
          <li ng-repeat="favo in arrayFavo | filter:selected" ng-click="itemFavoInbox(favo.first,favo.id)">
              {{favo.first}}
          </li>
      </ul>
      

      filter:{first: selected} 选择字段;

      Fiddle;

      【讨论】:

      • 好的,很好,但我真正的问题是,当我选择一个项目时,我需要隐藏项目列表,这是我的问题。
      猜你喜欢
      • 2022-08-16
      • 2023-04-01
      • 2015-12-25
      • 1970-01-01
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多