【发布时间】:2016-09-17 21:05:58
【问题描述】:
我正在使用 Angular-Meteor 和 Ionic。
Meteor 版本:1.3.2.4,Angular 版本:1.5.5,Ionic 版本:1.2.4。
我还在 Meteor/Angular 应用程序中使用 ES2015。
对于一种观点,我有:
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';
import { Meteor } from 'meteor/meteor';
import { SomeCollection } from 'path/to/collection';
import './view1.html';
class View1 {
constructor($scope, $reactive, $state) {
this.$state = $state;
this.$scope = $scope;
$reactive(this).attach($scope);
this.subscribe('my.subscription');
this.helpers({
list: () => {
return SomeCollection.find({});
}
});
}
}
export default angular.module('view1', [
angularMeteor,
uiRouter,
View1
]).config(config);
function config($stateProvider) {
'ngInject';
$stateProvider.state('app.view1', {
url: '/view1',
template: '<view-1></view-1>',
views: {
'appContent': {
controller: View1,
controllerAs: 'view1',
templateUrl: 'path/to/template.html'
}
}
});
}
另一个视图我有类似的设置,只是我订阅了不同的出版物。
类似:
import angular from 'angular';
import angularMeteor from 'angular-meteor';
import uiRouter from 'angular-ui-router';
import { Meteor } from 'meteor/meteor';
import { SomeOtherCollection } from 'path/to/other/collection';
import './view2.html';
class View2 {
constructor($scope, $reactive, $state) {
this.$state = $state;
this.$scope = $scope;
$reactive(this).attach($scope);
this.subscribe('my.other.subscription');
this.helpers({
list: () => {
return SomeOtherCollection.find({});
}
});
}
}
export default angular.module('view2', [
angularMeteor,
uiRouter,
View2
]).config(config);
function config($stateProvider) {
'ngInject';
$stateProvider.state('app.view2', {
url: '/view2',
template: '<view-2></view-2>',
views: {
'appContent': {
controller: View2,
controllerAs: 'view2',
templateUrl: 'path/to/other/template.html'
}
}
});
}
当我在 Ionic 视图之间切换时,我注意到我仍在从以前视图的订阅中进行轮询。根据文档,如果我使用$scope,则应该自动调用stop() 函数。
但是,我可以使用Mongol 看到我的订阅并没有停止切换视图。
我目前已经能够使用快速修复,但它很难看,并且强制使用我认为不必要的变通方法填充我的代码,即:
$scope.$on('$ionicView.enter', () => {
this.subscriptions = [];
this.subscriptions.push(this.subscribe('some.collection'));
this.subscriptions.push(this.subscribe('some.other.collection'));
});
$scope.$on('$ionicView.leave', () => {
for (var sub in this.subscriptions) {
this.subscriptions[sub].stop();
}
});
我做错了什么?在 Ionic 视图之间切换时如何正确停止订阅?
【问题讨论】:
-
我认为您的“变通办法”实际上是正确的模式...
标签: javascript angularjs meteor ionic-framework angular-meteor