AngularJS工作一次然后不再渲染

时间:2016-12-14 20:16:50

标签: javascript html angularjs

所以我试图从API调用中列出一些数据,即JSON。我是Angular和JavaScript的新手,我认为整个Async的东西都在弄乱我的逻辑。有时当我刷新页面时,它完美地工作,我期望的所有数据都存在,但是下一次我刷新它就消失了。当我在控制器中更改某些内容时,这似乎就会发生。

var app = angular.module("hasRead", ['hasRead.controllers', 'hasRead.directives', 'hasRead.services', 'hasRead.filters','ngRoute']);

app.config(["$routeProvider", function ($routeProvider) {
  $routeProvider
    .when("/books/:shelf", {controller: "BooksCtrl", templateUrl: "partials/books_list.html"})
    .when("/recomend", {controller: "RecomendCtrl", templateUrl: "partials/recomend.html"})
    .otherwise({redirectTo: "/books/read"})
}]);

controller.js

var ctrls = angular.module('hasRead.controllers', []);

ctrls.controller('AppCtrl', function ($scope) {
});

ctrls.controller('BooksCtrl', function ($scope, $book, $routeParams) {
  $scope.books;
  var shelf = $routeParams.list;
  $book.getBooks(shelf).then( function(data) {
    $scope.books = data.query.results.body.goodreadsresponse.reviews;
    console.log($scope.books);
    $scope.$apply;
  });
});

ctrls.controller('RecomendCtrl', function ($scope) {
  $scope.test = "testing";
});

HTML

<div>
  <book ng-repeat="review in books.review" review-data="review"></book>
</div>

这是一个相当多的代码所以这里是一个plunkr: https://plnkr.co/edit/l3A6QF?p=info

1 个答案:

答案 0 :(得分:0)

我在控制器的$scope.$apply();中没有括号。