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

all*_*ded 7 tags angularjs 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: 'Tag1' },
          { text: 'Tag2' },
          { text: 'Tag3' }
        ];

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

        scope.loadTags = function (query) {
          return $http.get('tags.json');
        }
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

'tag.html'中的HTML:

<tags-input ng-model="tags">
  <auto-complete source="loadTags($query)"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>
Run Code Online (Sandbox Code Playgroud)

工作图: TagExample


太棒了,但我不想使用,$http.get因为我已经有一个对象,里面有我想要用于自动完成的标签.所以我尝试了这个

角度指令代码:

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;
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

我的'tag.html'中的HTML:

<tags-input ng-model="tags">
  <auto-complete ng-model="loadTags"></auto-complete>
</tags-input>
<p>Model: {{tags}}</p>
Run Code Online (Sandbox Code Playgroud)

但这根本不起作用.相反,我得到了

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
Run Code Online (Sandbox Code Playgroud)

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

all*_*ded 18

所以需要更改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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

除此之外,您需要修复标记,以便它使用source选项:

<auto-complete source="loadTags()"></auto-complete>
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题