NgTagInput未显示自动搜索结果

时间:2015-06-21 15:16:02

标签: angularjs ng-tags-input

我正在尝试实施ngTagsInput,我希望在其中实现自动搜索电子邮件。为此我写下面的代码。但它不显示自动搜索结果

HTML:

<tags-input ng-model="compose.receiver"
            add-from-autocomplete-only="true" 
            min-length="1">
    <auto-complete source="loadReceiver($query)" 
                   min-length="0"
                   debounce-delay="0" 
                   max-results-to-show="10" 
                   loadOnEmpty="true">
    </auto-complete>
</tags-input>

控制器:

$scope.loadReceiver = function(query){
  return AdminInbox.loadReceiver(query);
}

服务:

angular.module('inboxes').factory('AdminInbox', ['$http','$q',
  function($http,$q) {
    return {
      loadReceiver: function(query) {
        console.log(query);
        var deferred = $q.defer();
        var receiver = $http.get('mailreceiver/'+query);
        console.log(receiver);
        return deferred.promise;
      }
    }
  }
]);

我成功获得以下格式的回复:

[{_id: "5579c9a4f3d71f8c2a4f1e3d"  email: "abc@gmail.com"},
{_id: "557f2cd3a571f9a41e4168f2"  email: "xyz@gmail.com"}]

1 个答案:

答案 0 :(得分:1)

拥有有效的JSON并设置display-property并且它有效!

angular.module('app', ['ngTagsInput'])
  .run(function($rootScope) {
    $rootScope.source = [{
      _id: "5579c9a4f3d71f8c2a4f1e3d",
      email: "abc@gmail.com"
    }, {
      _id: "557f2cd3a571f9a41e4168f2",
      email: "xyz@gmail.com"
    }];

    $rootScope.compose = {
      receiver: []
    };
  });
<link rel="stylesheet" href="http://mbenford.github.io/ngTagsInput/css/ng-tags-input.min.css" />

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-tags-input/2.3.0/ng-tags-input.js"></script>
<div ng-app="app">
  <tags-input ng-model="compose.receiver" display-property="email" add-from-autocomplete-only="true" min-length="1">
    <auto-complete source="source" min-length="0" debounce-delay="0" max-results-to-show="10" loadOnEmpty="true" displayProperty="email">
    </auto-complete>
  </tags-input>
</div>