gpt4 book ai didi

angularjs - 使用 ngTagsInput 自动完成无法读取未定义的属性 'then'

转载 作者:行者123 更新时间:2023-12-03 08:22:57 25 4
gpt4 key购买 nike

我试图找出这个问题,但我没有运气。

这是我写的有效的plunker。请注意,当我使用 $http.get 访问 tags.json 时,代码运行良好。

Angular 指令代码:

app.directive('tag', function($http) {
return {
restrict: 'E',
templateUrl: 'tag.html',
link: function (scope, el) {
scope.tags = [
{ text: 'Tag1' },
{ text: 'Tag2' },
{ text: 'Tag3' }
];

var test = [{ "text": "Tag9" },{ "text": "Tag10" }];

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

'tag.html' 内的 HTML:
<tags-input ng-model="tags">
<auto-complete source="loadTags($query)"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>

工作图片:
TagExample

很好,但是,我不想使用 $http.get因为我已经有一个对象,其中包含我想用于自动完成的标签。所以我试过这个

Angular 指令代码:
app.directive('tag', function($http) {
return {
restrict: 'E',
templateUrl: 'tag.html',
link: function (scope, el) {
scope.tags = [
{ text: 'Tag1' },
{ text: 'Tag2' },
{ text: 'Tag3' }
];

var test = [{ "text": "Tag9" },{ "text": "Tag10" }];
scope.loadTags = test;
}
}
});

我的“tag.html”中的 HTML:
<tags-input ng-model="tags">
<auto-complete ng-model="loadTags"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>

但这根本行不通。相反,我得到
TypeError: Cannot read property 'then' of undefined
at http://cdnjs.cloudflare.com/ajax/libs/ng-tags-input/2.0.0/ng-tags-input.min.js:1:5044
at http://code.angularjs.org/1.2.15/angular.js:13777:28
at completeOutstandingRequest (http://code.angularjs.org/1.2.15/angular.js:4236:10)
at http://code.angularjs.org/1.2.15/angular.js:4537:7 angular.js:9563

链接到我的 Plunk:
http://plnkr.co/edit/wEqVMf?p=info

最佳答案

因此需要更改 loadFunction 以便它返回一个 promise :

app.directive('tag', function($q) {
...
link: function(scope) {
$scope.loadTags = function() {
var deferred = $q.defer();
deferred.resolve([{ text: 'Tag9' },{ text: 'Tag10' }]);
return deferred.promise;
}
}
}

除此之外,您需要修复您的标记,以便它使用 source 选项:
<auto-complete source="loadTags()"></auto-complete>

这解决了我的问题

关于angularjs - 使用 ngTagsInput 自动完成无法读取未定义的属性 'then',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23069562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com