我在使用$ resource.query()方法时遇到了一些问题。我正在尝试访问其中的第一个元素,但它不适合我。选择控制器返回集合没有问题。
controller.js
angular.module('bookshelf').controller('bsMainCtrl', function($scope, bsBooks){
var data = bsBooks.query();
$scope.singleBook = data[0];
});
angular.module('bookshelf').controller('bsSelectionCtrl', function($scope, bsBooks){
var booksData = bsBooks.query();
$scope.books = booksData;
});
main.jade
.singleBook
.selection
include selection
.content
include book-list
图书-list.jade
div(ng-controller='bsMainCtrl')
p Title: {{ singleBook.title }}
p Author: {{ singleBook.author }}
p ISBN: {{ singleBook.ISBN }}
p Created at: {{ singleBook.createdAt | date }}
a(ng-href='/edit/{{ singleBook._id }}') Edit
selection.jade
div(ng-controller="bsSelectionCtrl")
div(ng-repeat='book in books')
button(ng-click='selectBook()') {{ book.title }}
service.js
angular.module('bookshelf').factory('bsBooks', function($resource) {
var BookshelfResource = $resource('/api/books/:_id', {_id: "@id" }, {
update: {method: 'PUT', isArray: false}
});
return BookshelfResource;
});
route.js
exports.getBooks = function(req, res){
Book.find({}).exec(function(err, collection){
res.send(collection);
});
};
Angular $范围是否相关?我尝试过使用Angular检查器,但只能得出结论:singleBook为null。