Following the Angular-Meteor tutorial step 9, I'm trying to create an Angular directive that uses a Meteor collection.
按照Angular-Meteor教程第9步,我正在尝试创建一个使用Meteor集合的Angular指令。
This file is in the root folder:
该文件位于根文件夹中:
TicTacToeBoards = new Meteor.Collection("tic_tac_toe_boards");
if (Meteor.isServer) {
Meteor.publish('TicTacToeBoards', function() { return TicTacToeBoards.find(); });
}
This file is in the /client folder:
该文件位于/ 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 });
}
});
}
}
}
Unfortunately, it gives an error:
不幸的是,它给出了一个错误:
TypeError: $scope.TicTacToeBoards.find is not a function
TypeError:$ scope.TicTacToeBoards.find不是函数
It appears that $scope.TicTacToeBoards
is not a Mongo cursor, but is the array of objects that TicTacToeBoards.find() would return. Why isn't it a cursor?
似乎$ scope.TicTacToeBoards不是Mongo游标,而是TicTacToeBoards.find()将返回的对象数组。为什么不是光标?
1 个解决方案
#1
1
You are right, $meteor.collection doesn't return a cursor, it returns an array of type AngularMeteorCollection which is different: http://angular-meteor.com/api/AngularMeteorCollection
你是对的,$ meteor.collection不返回游标,它返回一个不同类型的AngularMeteorCollection数组:http://angular-meteor.com/api/AngularMeteorCollection
It does that because we wanted to give Angular developers a regular array with ll it's API to work easily with.
这样做是因为我们希望为Angular开发人员提供一个常规数组,并使用它可以轻松使用它的API。
It's an interesting idea to add a find
function to that array though. Do want want to use that function to return a filtered object? You could use filters for that, but maybe we can add this option as well
但是,将查找函数添加到该数组是一个有趣的想法。是否想要使用该函数返回过滤的对象?您可以使用过滤器,但也许我们也可以添加此选项
#1
1
You are right, $meteor.collection doesn't return a cursor, it returns an array of type AngularMeteorCollection which is different: http://angular-meteor.com/api/AngularMeteorCollection
你是对的,$ meteor.collection不返回游标,它返回一个不同类型的AngularMeteorCollection数组:http://angular-meteor.com/api/AngularMeteorCollection
It does that because we wanted to give Angular developers a regular array with ll it's API to work easily with.
这样做是因为我们希望为Angular开发人员提供一个常规数组,并使用它可以轻松使用它的API。
It's an interesting idea to add a find
function to that array though. Do want want to use that function to return a filtered object? You could use filters for that, but maybe we can add this option as well
但是,将查找函数添加到该数组是一个有趣的想法。是否想要使用该函数返回过滤的对象?您可以使用过滤器,但也许我们也可以添加此选项