A service that binds an angular $scope variable to a Meteor Session variable, so it can be interacted (get, set) from within Angular modules as well as Meteor modules (templates).
$meteor.sessionhas been deprecated in favor of of using regular Meteor Session directly without any wrappers.Note that you are no longer be able to bind
$scopeto your session. if you are using sessions in order to getReactive Vars, then it’s better that you will use reactive vars in scope getReactively.Here is an example for how to migrate:
Old code:
angular.module(`myApp`, []).controller(`MyCtrl`, function($scope, $meteor) {
$scope.myModel = 20;
$meteor.session(`mySession`).bind($scope, `myModel`);
});
New code:
angular.module(`myApp`, []).controller(`MyCtrl`, function($scope) {
Session.set(`mySession`, `myValue`);
$scope.helpers({
myModel() {
return Session.get(`mySession`);
}
});
});
$meteor.session(sessionKey).bind(scope, model)
| Param | Type | Details | Required |
|---|---|---|---|
| sessionKey | string | The name of the session variable |
Yes |
| scope | Scope | The scope the document will be bound to |
Yes |
| model | String | The name of the scope's model variable that the document will be bound to |
Yes |
JavaScript:
if (Meteor.isClient) {
app.controller("MainCtrl", ['$scope', '$meteor',
function($scope, $meteor){
$meteor.session('counter').bind($scope, 'counter');
Tracker.autorun(function () {
console.log('angular counter changed', Session.get('counter'));
});
}]);
}
Template.hello.events({ // change to variable from outside angular
'click button': function () {
// increment the counter when button is clicked
Session.set("counter", Session.get("counter") + 1);
}
});
}
HTML:
<div ng-controller="MainCtrl">
angular counter = [[ counter ]]
<button ng-click="counter=counter+1">Add angular counter</button>
<button>Add Meteor counter</button>
</div>