7

我正在尝试使用 Meteor 和 Angular.js 的组合在我的 MongoDb 中获取某个地址的警告

在我的 html 文件中,我正在做

<div ng-controller = "myController as myCtrl">
{{myCtrl.warnings}}
{{myCtrl.getWarnings("123 Test Street, TestCity, TestState")}}
</div>

在我的 app.js 文件中:

Warnings = new Mongo.Collection("Warnings");

if (Meteor.isClient) {
  var app = angular.module('ffprototype', [ 'angular-meteor' ]);

  app.controller('myController', ['$window','$meteor', function($window, $meteor) {

    this.warnings = $meteor.collection(Warnings);

    this.getWarnings = function(findByAddress){
        Warnings.find({address: findByAddress}).fetch();
    }
  }]);
}

我的 mongoDb 集合:

{
    "_id": "3ixgxEMZDWGtugxA7",
    "address": "123 Test Street, TestCity, TestState",
    "warning": "Warning 1"
}
{
   "_id": "HZH5FvCD5driBYSJz",
    "address": "123 Test Street, TestCity, TestState",
    "warning": "Warning 2"
}

html 网页的输出显示了整个警告集合(感谢{{currentDispatch.warnings}},但没有显示任何内容){{currentDispatch.getWarnings("123 Test Street, TestCity, TestState")}}

4

2 回答 2

6

您应该为此使用$meteor.object

this.getWarnings = function(findByAddress){
  $meteor.object(Warnings, { address: findByAddress }, false); // passing false here to not update the collection from changes in the client
}
于 2015-05-11T19:35:55.897 回答
0

从 angular-meteor docs看来,它$meteor.object很快就会被弃用。

不再需要$meteor.object了,因为我们可以像这样使用 Mongo Collection 的findOne功能。

旧代码:

$scope.party = $meteor.object(Parties, $stateParams.partyId);

新代码:

$scope.helpers({
  party() {
    return Parties.findOne($stateParams.partyId);
  }
});

更详细的绑定一教程

于 2016-03-19T15:33:53.307 回答