我正在使用ng-tags-input将标记添加到Angular + Firebase应用程序。
尝试利用自动填充功能自动填充firebaseArray中的标记时遇到以下错误,该标记将存储在我的firebase中的现有标记的值拉为/ tags
ERROR
TypeError: Cannot read property 'data' of undefined
这是我的代码......
HTML
<div class="form-group">
<label for="inputTags" class="col-lg-2 control-label">Tags</label>
<div class="col-lg-3">
<tags-input ng-model="post.tags" >
<auto-complete source="loadTags()"
min-length="1"
load-on-focus="true"
load-on-empty="true"
max-results-to-show="32"></auto-complete>
</tags-input>
</div>
</div>
JAVASCRIPT
$scope.loadTags = function() {
var tagsArray = ['test']; //simply a test value
var tagsRef = new Firebase(FIREBASE_URL + 'tags');
tagsRef.once('value').then(function(snapshot) {
// The Promise was "fulfilled" (it succeeded).
console.log('Promise was fulfilled');
// handle data
angular.forEach (snapshot.val(), function(tag) {
tagsArray.push(tag.name);
});
console.log(tagsArray);
return tagsArray
}, function(error) {
// The Promise was rejected.
console.log('Promise was rejected');
console.error(error);
});
//return tagsArray; <-- This Works, but doesn't include firebaseArray values
};
正如您所看到的,我正在使用最新的Firebase版本(2.4.0)并在我的firebase查询中使用Promise ...如果您查看控制台输出,您将看到错误发生后履行承诺的位置触发......
TypeError: Cannot read property 'data' of undefined
at ng-tags-input.js:590
at processQueue (angular.js:14792)
at angular.js:14808
at Scope.$eval (angular.js:16052)
at Scope.$digest (angular.js:15870)
at Scope.$apply (angular.js:16160)
at angular.js:17927
at completeOutstandingRequest (angular.js:5552)
at angular.js:5829(anonymous function) @ angular.js:12520(anonymous function) @ angular.js:9292processQueue @ angular.js:14800(anonymous function) @ angular.js:14808Scope.$eval @ angular.js:16052Scope.$digest @ angular.js:15870Scope.$apply @ angular.js:16160(anonymous function) @ angular.js:17927completeOutstandingRequest @ angular.js:5552(anonymous function) @ angular.js:5829
posts.controller.js:15 Promise was fulfilled
posts.controller.js:22 ["test", "firebase", "javascript", "node.js", "angular.js", "bower.js", "npm", "angular.js", "bootstrap"]
QUESTION吗
有没有办法配置ng-tags-input以异步方式运行,只有在履行承诺后才加载值?
我将不胜感激任何帮助或建议。
干杯! @FellowHobbyist
答案 0 :(得分:1)
ngTagsInput可以很好地处理承诺。您需要做的就是从library(data.table)
acast(setDT(pat1)[, SUB2:= rev(SUB), PATID], SUB~SUB2,
value.var='PATID', length)
# 2A 2B 2C 2D
#2A 0 1 0 0
#2B 1 0 0 0
#2C 0 0 0 1
#2D 0 0 1 0
函数返回一个承诺:
loadTags
另请注意我在您的代码中包含的$scope.loadTags = function() {
var tagsRef = new Firebase(FIREBASE_URL + 'tags');
return tagsRef.once('value').then(function(snapshot) {
console.log('Promise was fulfilled');
var tagsArray = ['test'];
angular.forEach (snapshot.val(), function(tag) {
tagsArray.push(tag.name);
});
console.log(tagsArray);
return tagsArray;
}, function(error) {
console.log('Promise was rejected');
console.error(error);
return $q.reject(); // <== This is important!
});
};
。当您提供错误回调时,您必须返回被拒绝的承诺,否则整个承诺将被视为已履行。