2015-05-09 55 views
7

我想获得在我的MongoDB中的某个地址的警告,使用流星和Angular.js角流星发现MongoDB的收集和返回基于PARAMS

在我的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")}}

回答

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 
} 
+0

如果我这样做,我不会放松自动发布功能吗? 'this.warnings = $ meteor.collection(Warnings);'自动将更新推送到数据库,但是当我必须“返回”时,那些东西不会丢失? –

+0

你是对的,看我的编辑 – tmaximini

+0

谢谢我回家后试试看,如果这个工程我会接受,你会得到你的要点 –

0

从角流星docs,它出现$meteor.object即将被弃用。

因为我们可以使用Mongo Collection的findOne函数,所以没有必要使用$meteor.object

旧代码:

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

新代码:

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

更详细bind one tutorial