【问题标题】:ng-tags-input autocomplete display non query matched resultng-tags-input 自动完成显示无 jquery 匹配结果
【发布时间】:2016-09-04 22:12:11
【问题描述】:

查看:

<div class="row" ng-controller="TagsInputController">
    <tags-input ng-model="tags">
       <auto-complete source="queryTags($query)" min-length="1"></auto-complete>
    </tags-input>
</div>

控制器:

myApp.controller('TagsInputController',['$scope','$timeout','$http',function($scope,$timeout,$http){
      $scope.tags = [
    { text: 'Tag1' },
    { text: 'Tag2' },
    { text: 'Tag3' }
  ];


  $scope.queryTags=function($query){
    return $http.get('tags.php',{
        params:{
            'tag':$query
        }
    })
  }

}]);

php:tags.php

<?php
$names=array(
    'palash',
    'kailash',
    'kuldeep'
    );

echo json_encode($names); ?>

请查看我附加的输出,它显示了那些与查询不匹配的标签,我只想显示匹配的标签

【问题讨论】:

  • 发布您的代码,以便我们为您提供帮助。
  • 请再次阅读我的问题,我添加了代码

标签: angularjs ng-tags-input


【解决方案1】:

这段代码

$scope.queryTags=function($query){
    return $http.get('tags.php',{
        params:{
            'tag':$query
        }
    })
}

只会从tags.php 返回您的姓名,而不对其进行任何过滤,而不是在服务器上和客户端上。 您可以使用Array.prototype.filterArray.prototype.includes 方法根据$query 过滤您的数组

$scope.queryTags=function($query){
    return $http.get('tags.php',{
        params:{
            'tag':$query
        }
    }).then(function(names) {
        var filteredNames = names.filter(function(name) {
            return (name.includes($query);
        });
        return $q.when(filteredNames);
    })
}

【讨论】:

    【解决方案2】:

    自动完成只返回 promise 给出的输出。要应用过滤,您必须自己提供方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多