0

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() 将返回的对象数组。为什么不是游标?

4

1 回答 1

1

你是对的, $meteor.collection 不返回游标,它返回一个不同类型的 AngularMeteorCollection 数组:http: //angular-meteor.com/api/AngularMeteorCollection

这样做是因为我们想为 Angular 开发人员提供一个常规数组,并且它的 API 可以轻松使用。

find不过,向该数组添加一个函数是一个有趣的想法。想要使用该函数返回过滤后的对象吗?您可以为此使用过滤器,但也许我们也可以添加此选项

于 2015-07-19T11:24:01.613 回答