angular-meteor根据params

时间:2015-05-09 13:52:32

标签: angularjs mongodb meteor angular-meteor

我正在尝试使用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();
    }
  }]);
}

my mongoDb collection:

{
    "_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")}}

没有显示任何内容

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:0)

从角度流星docs,似乎很快就会弃用$meteor.object

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

旧代码:

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

新代码:

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

更详细bind one tutorial