0

見る:

<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); ?>

出力

添付した出力を参照してください。クエリに一致しないタグが表示されています。一致したタグのみを表示したい

4

2 に答える 2

1

このコード

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

tags.phpサーバー上でもクライアント上でも、フィルタリングを行わずに名前を返すだけです。Array.prototype.filterおよびArray.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);
    })
}
于 2016-09-08T09:18:44.853 に答える