[英]AngularJS : Restangular Library
我正在尝试神话般的Restangular库,尝试返回一些JSON对象并将它们绑定到$ scope对象时遇到了一些麻烦。
我的Factory脚本中有此函数,该脚本本质上只是利用Restangular.getList()函数返回博客列表。
angular.module("ecmaworld").factory("Authenticate", function(Restangular) {
// Testing Function
var Blogs = Restangular.all("blogs");
return {
blogs: function() {
Blogs.getList("blogs").then(function(blogs) {
return blogs;
}, function(error) {
console.log(error);
});
}
};});
这里的Factory方法返回博客文章列表。 然后,我尝试调用此函数,该函数是在Factory脚本中定义的,并在我的一个控制器脚本中使用它,如下所示:
angular.module("ecmaworld").controller("LoginController", function($scope, Authenticate) {
//Testing function
$scope.getBlogs = function() {
$scope.blogs = Authenticate.blogs();
};
});
当我尝试使用“ data-ng-repeat”指令遍历所有博客文章时,没有任何显示。 我看到HTTP响应返回JSON对象时,该API方法已通过Google开发人员使用。 但是,我不确定为什么尝试在VIEW中循环浏览它们时为什么不显示它们。 如果您能帮助我,将不胜感激。 我只是Angular的新手,所以轻松一点。
谢谢。
blogs()
方法需要返回承诺...
blogs: function() {
return Blogs.getList("blogs").then(function(blogs) {
return blogs;
}, function(error) {
console.log(error);
});
}
然后在解决承诺后将结果分配给scope属性...
$scope.getBlogs = function() {
Authenticate.blogs().then(function (blogs) {
$scope.blogs = blogs;
});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.