【问题标题】:ng-tags-input autocompete not workingng-tags-input 自动竞争不起作用
【发布时间】:2016-05-28 07:53:19
【问题描述】:

我想将ng-tags-input 与自动完成功能一起使用,但出现错误:

e.then 不是函数

这是我的 HTML

<tags-input ng-model="selectedBodyParts" class="ui-tags-input" add-on-paste="true">
      <auto-complete source="loadBodyParts($query)" min-length="0" load-on-focus="true">

      </auto-complete>
</tags-input>

还有 AngularJS 代码:

    $scope.loadBodyParts = function ($query){
        var bodypartList = angular.copy($scope.bodyParts);
        return bodypartList.filter(function(bodypart) {
            return bodypart.bodyPartName.toLowerCase().indexOf($query.toLowerCase()) !== -1;
        });
};

任何帮助将不胜感激!

【问题讨论】:

  • 我想知道你是否可以提供一个 plunker 来显示你的问题?你可以使用this template

标签: angularjs ng-tags-input


【解决方案1】:

您使用 ng-tag-input 加载源的函数应该返回一个 Promise 对象,Promise 有一个带有签名 .then(fn, fn) 的函数,这可能是它正在寻找但没有找到的,因此是你的。 .

e.then is not a function 错误

如果你去demos section,在“带自动完成的简单标签输入”下,你会看到这段代码

$scope.loadTags = function(query) {
    return $http.get('tags.json');
};

在角度$http.get 返回一个承诺。如果我必须更改您的代码,我可能会使用$q 来承诺您的数据并返回一个包含您过滤数据的承诺。

$scope.loadBodyParts = function ($query){
    var bodypartList = angular.copy($scope.bodyParts);
    var promisifyMe = bodypartList.filter(function(bodypart) {
        return bodypart.bodyPartName.toLowerCase().indexOf($query.toLowerCase()) !== -1;
    });
    return $q.when(promisifyMe);
};

$q docs

【讨论】:

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