首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >angular-meteor根据参数查找MongoDb集合和返回

angular-meteor根据参数查找MongoDb集合和返回
EN

Stack Overflow用户
提问于 2015-05-09 21:52:32
回答 2查看 1.7K关注 0票数 7

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

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

代码语言:javascript
复制
<div ng-controller = "myController as myCtrl">
{{myCtrl.warnings}}
{{myCtrl.getWarnings("123 Test Street, TestCity, TestState")}}
</div>

在app.js文件中:

代码语言:javascript
复制
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收藏:

代码语言:javascript
复制
{
    "_id": "3ixgxEMZDWGtugxA7",
    "address": "123 Test Street, TestCity, TestState",
    "warning": "Warning 1"
}
{
   "_id": "HZH5FvCD5driBYSJz",
    "address": "123 Test Street, TestCity, TestState",
    "warning": "Warning 2"
}

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

EN

回答 2

Stack Overflow用户

发布于 2015-05-12 03:35:56

为此,您应该使用$meteor.object

代码语言:javascript
复制
this.getWarnings = function(findByAddress){
  $meteor.object(Warnings, { address: findByAddress }, false); // passing false here to not update the collection from changes in the client
}
票数 6
EN

Stack Overflow用户

发布于 2016-03-19 23:33:53

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

不再需要$meteor.object,因为我们可以使用Mongo Collection的findOne函数,如下所示。

旧代码:

代码语言:javascript
复制
$scope.party = $meteor.object(Parties, $stateParams.partyId);

新代码:

代码语言:javascript
复制
$scope.helpers({
  party() {
    return Parties.findOne($stateParams.partyId);
  }
});

更详细的bind one tutorial

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30140736

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档