在Angular-Meteor 教程第 9 步之后,我正在尝试创建一个使用 Meteor 集合的 Angular 指令。
此文件位于根文件夹中:
TicTacToeBoards = new Meteor.Collection("tic_tac_toe_boards");
if (Meteor.isServer) {
Meteor.publish('TicTacToeBoards', function() { return TicTacToeBoards.find(); });
}
此文件位于 /client 文件夹中:
angular.module('TicTacToe').directive('tictactoegraph', function() {
return {
templateUrl: 'client/graph/tictactoegraph.ng.html',
scope: true,
controller: function($scope, $meteor, Sigma, TicTacToeClass) {
$scope.TicTacToeBoards = false;
$meteor.subscribe('TicTacToeBoards').then(function(subscriptionHandle){
$scope.TicTacToeBoards = $meteor.collection(TicTacToeBoards);
});
},
link: function($scope, element, attrs) {
// TODO: Ask SO if there's a better way to wait on the subscription....
$scope.$watch('TicTacToeBoards', function(newValue, oldValue) {
if ($scope.TicTacToeBoards) {
console.log($scope.TicTacToeBoards); // An array of objects.
var nextBoards = $scope.TicTacToeBoards.find({ numberOfMoves: 0 });
}
});
}
}
}
不幸的是,它给出了一个错误:
TypeError: $scope.TicTacToeBoards.find 不是函数
看起来这$scope.TicTacToeBoards
不是 Mongo 光标,而是 TicTacToeBoards.find() 将返回的对象数组。为什么不是游标?