Angularjs和流星"会话"反应性,有没有办法? [英] Angularjs and Meteor "Session" reactivity, is there a way?

查看:204
本文介绍了Angularjs和流星"会话"反应性,有没有办法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用流星和Angularjs工作。我使用 Meteor_angularjs 包,这与收藏作品确定

I'm trying to work with Meteor and Angularjs. I'm using Meteor_angularjs package, which works OK with Collections.

现在我试图使用会话和我的反应数据存储:

Now I'm trying to use Session and my reactive data store:

TestCtrl = [
    "$scope",
    function($scope){
        $scope.value = Session.get('someValue');
    }
]

这是行不通的。

问:任何关于如何牵制流星的会话和角度建议?

QUESTION: Any suggestions on how to tie down Meteor's Session and Angular?

据我了解,我可以写指令,将轮询会话几乎每ofter,但是我不认为这是一个不错的选择。结果
谢谢

As far as I understand, I can write directive that will be polling Session every so ofter, however I don't think that's a good choice.
Thanks

更新:

我试过以下内容:

TestCtrl = [
    "$scope",
    function($scope){
        Meteor.autorun(function(){
            $scope.config = Session.get('testsConfig');
            if (!$scope.$$phase){
                $scope.$digest();
            }
        });
    }
]

和它的排序工作,但是我得到以下错误:

and it sort of works, however I get the following error:

Error: INVALID_STATE_ERR: DOM Exception 11
Error: An attempt was made to use an object that is not, or is no longer, usable.
    at derez (http://localhost:3000/test:95:41)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30)
    at derez (http://localhost:3000/test:95:30) angular.js:5526
$get angular.js:5526
$get angular.js:4660
$get.Scope.$digest angular.js:7674
(anonymous function) controllers.js:46
Meteor.autorun.rerun deps-utils.js:78
_.extend.run deps.js:19
Meteor.autorun.rerun deps-utils.js:78
_.extend.flush deps.js:63
_.each._.forEach underscore.js:79
_.extend.flush deps.js:61
_.each._.forEach underscore.js:79
_.extend.flush deps.js:60

更新2:

我已经试过像这样的服务,(可能是错误的用法),仍然一无所获。现在,它不会更新所有的会话值的变化。

I've tried the service like this (might be wrong usage), still nothing. Now it doesn't update at all on Session value's changes.

Meteor.autorun(function(){
    app.factory('ssn', function(){ return{
        get: function(val){
            return Session.get(val);
        }
    }});
});
TestCtrl = [
    "$scope","ssn",
    function($scope, ssn){
        $scope.config = ssn.get('testsConfig');
    }
]

更新3 :角有 $适用()

在角从角框架外执行前pression。 (例如,从浏览器的DOM事件,setTimeout的,XHR或第三方库)

to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries)

与此同时流星有 Meteor.render()

At the same time Meteor has Meteor.render() for

大部分的时间,但是,你不会直接调用这些函数 - 你只使用自己喜欢的模板包,如把手或翡翠。渲染和renderList功能适用于正在实施新的模板系统的人。

Most of the time, though, you won't call these functions directly — you'll just use your favorite templating package, such as Handlebars or Jade. The render and renderList functions are intended for people that are implementing new templating systems.

不过,好像我只是不能把2和2在一起。 (

However, it seems like I just cannot put 2 and 2 together. :(

推荐答案

这与旧的答案,但我看到有人提到它所以这里一个老问题是更新的答案。

this as an old question with old answers but I see people referring to it so here is the updated answer.

首先 - 有一个新库以角流星处理这些情况你

First - there is a new library for angular-meteor that handles those cases for you.

和本库提供了两个可能的解决方案:

And this library gives you two possible solutions:


  1. 如果你想要一个Session变量的作用域变量绑定,使用 $ meteorSession 服务。
    它所做的是,每一个范围变量会发生变化的时候,它会变成会话变量(如果它里面放置一个触发自动运行)。
    和每一个会话变量将改变时间,范围变量也会随之改变(和更改认为它放在)。

  1. If you want to bind a Session variable to a scope variable, use the $meteorSession service. What it does is that every time the scope variable will change, it will change to Session variable (and trigger an autorun if it's placed inside one). and every time the Session variable will change, the scope variable will change as well (and change the view that it's placed upon).

如果您正在使用Session变量只是为了获得一个变量的反应(即触发自动运行),你应该使用的 getReactively 。这仅返回现有范围的变量,但每次发生变化时触发自动运行。一个很好的例子可以发现,我们的教程

If you are using the Session variable just to get a variable reactive (meaning trigger an autorun), you should use getReactively . this just returns the already existing scope variable but trigger an autorun every time it changes. a good example of this can be found it our tutorial.


  • 注:反正,当您使用Tracker.autorun里面角,你需要将它连接到一个范围。这可以,如果你与 $ meteorUtils自动运行功能
  • 替换Tracker.autorun轻松完成
  • Note: In anyway, when you use Tracker.autorun inside Angular, you need to connect it to a scope. this can be easily done if you replace Tracker.autorun with the $meteorUtils autorun function

这篇关于Angularjs和流星"会话"反应性,有没有办法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆