0

我正在尝试使用urigo:angularwithdburles:collection-helpers但我无法弄清楚语法应该是什么。

这就是我只使用 Meteor 的:

Template.books.helpers({  
    books: function() {
        return Books.find();
    },
    authors: function() {
        return Authors.find();
    },
    author: function() {
        return Authors.findOne(this.authorId);
    }
});

在视图中:

<template name="books">  
    <h1>Books!</h1>
    <ol>
        {{#each books}}
            <li>{{name}} by {{author.name}}</li>
        {{/each}}
    </ol>
</template>

那工作得很好。

现在我已经添加了 Angular 并且我正在更改为这个(作者不起作用):

$scope.books = $meteor.collection(function(){
    return Books.find({})
});

$scope.authors = $meteor.collection(function(){
    return Authors.find({})
});

$scope.author = $meteor.collection(function(){
    return Authors.findOne(this.authorId);
});

对此的看法:

<li ng-repeat="book in books">{{book.name}} by {{author.name}}</li>

谢谢

4

1 回答 1

2

在角度中,您将结果绑定到$scope.author而不是函数本身。当你打电话时

$scope.author = $meteor.collection(function() {
    return Authors.findOne(this.authorId);
}

this指的是控制器(实际上我认为它指的是收集方法......无论哪种方式),而不是书。另外,angular-meteor没有findOne方法,你简单添加{limit: 1}或使用$meteor.object

要获得与您正在执行的操作类似的结果,您可以在控制器中执行以下操作:

function MainController($scope, $meteor) {
    $scope.books = $meteor.collection(function () {
        return Books.find({});
    });

    $scope.authors = $meteor.collection(function () {
        return Authors.find({});
    });

    $scope.author = function (book) {
       return $meteor.object(Authors, book.authorId, false);
    }
}

$meteor.object是您将使用的,而不是 findOne。现在在 html 中,您需要调用该函数:

<li ng-repeat="book in books">{{book.name}} by: {{author(this.book).name}}</li>

该函数是返回作者对象,当然你访问的名称。

这是一个流星垫

于 2015-08-17T00:25:39.633 回答