[英]Can't bind to angular repository data with promise
I'm trying to use the repository pattern with Angular. 我正在尝试将存储库模式用于Angular。 When I call my repository, I can see that the json is returned over the network.
调用存储库时,可以看到json是通过网络返回的。 But I can't bind to it.
但是我不能束缚它。
I'm injecting the repository into the module... what's wrong? 我正在将存储库注入模块中...出了什么问题?
blogApp.factory('blogEntryRepository', function ($http, $q) {
return {
get: function() {
var deferred = $q.defer();
$http.get('/blogentry').success(deferred.resolve).error(deferred.reject);
return deferred.promise;
}
}
});
blogApp.controller("HomeCtrl", function($scope, blogEntryRepository) {
blogEntryRepository.get().then($scope.blogEntries = blogEntryRepository);
});
<div>
home
<div ng-repeat="blogEntry in blogEntries">
<div>Asdf
{{blogEntry.title}}
</div>
</div>
</div>
The page outputs 页面输出
home
Asdf
{}
The json response is json响应是
[{"title":"Launched an AngularJS 1.2 site!","date":"2014-05-21T00:00:00"},{"title":"Title2","date":"2014-05-22T00:00:00"}]
It's within an ng-app tag. 它在ng-app标签内。 There's an ng-view tag on the page.
页面上有一个ng-view标签。
I can confirm that I can bind to a hard coded variable. 我可以确认可以绑定到硬编码变量。
Using Angular 1.2 使用Angular 1.2
try 尝试
blogApp.controller("HomeCtrl", function($scope, blogEntryRepository) {
blogEntryRepository.get().then(function(result){$scope.blogEntries = result});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.