我正在尝试使用Meteor和Angularjs。我正在使用Meteor_angularjs包,它与Collections
配合使用。
现在我正在尝试使用Session
和我的反应数据存储:
TestCtrl = [
"$scope",
function($scope){
$scope.value = Session.get('someValue');
}
]
这不起作用。
问题:有关如何绑定Meteor Session
和Angular的任何建议?
据我了解,我可以编写指令,每次都会轮询Session
,但我不认为这是一个不错的选择。
感谢
更新
我尝试了以下内容:
TestCtrl = [
"$scope",
function($scope){
Meteor.autorun(function(){
$scope.config = Session.get('testsConfig');
if (!$scope.$$phase){
$scope.$digest();
}
});
}
]
它有点工作,但是我收到以下错误:
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:
我尝试过这样的服务(可能是错误的用法),但仍然没有。现在它根本没有更新会话值的更改。
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 :Angular有$apply()
从角度框架外部以角度执行表达式。 (例如,来自浏览器DOM事件,setTimeout,XHR或第三方库)
同时Meteor有Meteor.render()
但大多数情况下,您不会直接调用这些函数 - 您只需使用自己喜欢的模板包,例如Handlebars或Jade。 render和renderList函数适用于正在实现新模板系统的人。
然而,似乎我不能把2和2放在一起。 :(
答案 0 :(得分:2)
这是一个旧答案的老问题,但我看到有人提到它,所以这里是更新后的答案。
首先 - 角度流星有一个new library可以为你处理这些情况。
这个库为您提供了两种可能的解决方案:
如果要将Session变量绑定到范围变量,请使用$meteorSession服务。 它的作用是每次范围变量发生变化时,它都会变为Session变量(如果它置于一个变量内,则触发自动运行)。 每当Session变量发生变化时,范围变量也会发生变化(并改变它所放置的视图)。
如果您正在使用Session变量来获取变量被动(意味着触发自动运行),则应使用getReactively。这只返回已存在的范围变量,但每次更改时都会触发自动运行。我们可以在tutorial找到一个很好的例子。
答案 1 :(得分:1)
嗨,这是一个选项(可能不是最好的,但我认为它有效)
app.service('Session',function($rootScope){
var self = this;
self.objects = {};
self.get = function(name){
self.objects[name] = {"value" : Session.get(name)};
Meteor.autorun(function() {
var i = Session.get(name);
if(self.objects[name].value != i){
if (!$rootScope.$$phase){
$rootScope.$apply(function(){
self.objects[name].value = i;
});
}
}
});
return self.objects[name];
}
self.set = function(name,value){
self.objects[name].value = value;
Session.set(name,value);
}
return self;
});
像这样在$ scope中调用它 $ scope.test = Session.get(“test”);
在{{test.value}}视图中。对不起,迟到了。
新年快乐!答案 2 :(得分:0)
试
angular.module('yourmod', [])
.controller('TestCtrl', ['$scope', function($scope) {
var c = Deps.autorun(function (comp) {
//... put reactive stuf on scope.....
if(!comp.firstRun) {
// only do not do aply at first run becaulse then apply is already running.
$scope.$apply()
}
});
// and to realy make it nice...
$scope.on('$destroy', function () {c.stop()});
}])