Angularjs 使用ngTagsInput自动完成无法读取属性';然后';未定义的
我正试图解决这个问题,但我运气不好 这就是我写的那本书。请注意,当我使用$http.get访问tags.json时,代码工作得非常好 角度指令代码:Angularjs 使用ngTagsInput自动完成无法读取属性';然后';未定义的,angularjs,tags,angularjs-directive,angularjs-scope,Angularjs,Tags,Angularjs Directive,Angularjs Scope,我正试图解决这个问题,但我运气不好 这就是我写的那本书。请注意,当我使用$http.get访问tags.json时,代码工作得非常好 角度指令代码: app.directive('tag', function($http) { return { restrict: 'E', templateUrl: 'tag.html', link: function (scope, el) { scope.tags = [ { text: 'Tag
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');
}
}
}
});
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 source="loadTags($query)"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>
<tags-input ng-model="tags">
<auto-complete ng-model="loadTags"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>
“tag.HTML”中的HTML:
<tags-input ng-model="tags">
<auto-complete source="loadTags($query)"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>
<tags-input ng-model="tags">
<auto-complete ng-model="loadTags"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>
链接到我的Plunk:
因此需要更改loadFunction,以便它返回承诺:
app.directive('tag', function($q) {
...
link: function(scope) {
$scope.loadTags = function() {
var deferred = $q.defer();
deferred.resolve([{ text: 'Tag9' },{ text: 'Tag10' }]);
return deferred.promise;
}
}
}
除此之外,您还需要修复标记,使其使用源选项:
<auto-complete source="loadTags()"></auto-complete>
这解决了我的问题你能解决这个问题吗?我也有同样的问题。API使用数组响应,但未定义“then”