gpt4 book ai didi

javascript - Ionic - 什么时候应该调用 ionicModal.remove()? [Ionic Modal 隐藏回调]

转载 作者:行者123 更新时间:2023-11-29 14:43:10 24 4
gpt4 key购买 nike

在我的 Controller 中,模态视图是通过监听 $ionicView.afterEnter 事件用工厂初始化的。作为documentation建议,只要当前事件的 ionic View 即将被销毁,就应该删除模态视图。在 $ionicView.beforeLeave 回调中调用了一个函数以移除模态视图。

.controller('UserProfileCtrl', function($scope, $state, user, ModalFactory) {
$scope.user = user;

$scope.checkOrders = function() {
$state.go('app.orders');
};

$scope.$on('$ionicView.afterEnter', function() {
$scope.modals = ModalFactory.getUserProfileModals($scope, user.photos);
});

$scope.$on('$ionicView.beforeLeave', function() {
$scope.modals.remove();
});
});

.factory('ModalFactory', function($ionicModal, $ionicSlideBoxDelegate) {
var modalFactory = {};

modalFactory.getUserProfileModals = function(scope, images) {
var modals = {
'views': [],
'data': []
};

$ionicModal.fromTemplateUrl('templates/modals/common/image-view.html', { 'scope': scope }).then(function(modal) { modals.views.imageView = modal; });

if (images) {
modals.data.images = images;
}

modals.open = function(view, slide) {
Object.keys(this.views).forEach(function(key, index) {
console.log(key);
});

if (view && this.views.hasOwnProperty(view)) {
this.views[view].show();

if (view == 'imageView' && slide) {
$ionicSlideBoxDelegate.$getByHandle('image-view').slide(slide);
}
}
};

modals.close = function(view) {
Object.keys(this.views).forEach(function(key, index) {
console.log(key);
});

if (view && this.views.hasOwnProperty(view)) {
this.views[view].hide();
} else {
Object.keys(this.views).forEach(function(key, index) {
this.views[key].hide();
});
}
};

modals.remove = function() {
console.log('remove');
Object.keys(this.views).forEach(function(key, index) {
console.log(key);
});

this.data.splice(0, this.data.length);

Object.keys(this.views).forEach(function(key, index) {
this.views[key].remove();
});

this.views.splice(0, this.views.length);
};

return modals;
};

return modalFactory;
});

但是,当我按顺序执行这些操作时,我在控制台中得到以下输出:

  1. 调用$scope.modals.open('imageView', 1),
  2. 调用$scope.modals.close(),
  3. 使用 $state.go('app.orders') 导航到另一个页面。

Screenshot showing console output

我试过听 $destroy 而不是 $ionicView.beforeLeave,但是 $scope.modals.remove() 没有被调用根本。当我用 Chrome 测试我的应用程序时,似乎 $destroy 没有被触发。

谁能告诉我什么时候应该删除模态视图,为什么在我的场景中会出现这样的错误消息?


更新

我修改了ModalFactory中的代码如下,错误没有了。

function open(view, slide) {
if (view && modals.views.hasOwnProperty(view)) {
modals.views[view].show();

if (view == 'imageView' && slide) {
$ionicSlideBoxDelegate.$getByHandle('image-view').slide(slide);
}
}
};

function close(view) {
if (view && modals.views.hasOwnProperty(view)) {
modals.views[view].hide();
} else {
Object.keys(modals.views).forEach(function(key, index) {
modals.views[key].hide();
});
}
};

function remove() {
console.log('remove');

modals.data.splice(0, modals.data.length);

Object.keys(modals.views).forEach(function(key, index) {
modals.views[key].remove();
});

modals.views.splice(0, modals.views.length);
};

modals.open = open;
modals.close = close;
modals.remove = remove;

return modals;

最佳答案

通过使用 $scope.$on('$destroy', ..) 方法,Angular 将在拆除作用域并从其父作用域中删除作用域之前广播一个 $destroy 事件。

这是我如何解决 modal.remove() 问题的示例,

.factory('ModalFactory', function($ionicModal, $rootScope) {
var init = function(tpl, $scope) {
var promise;
$scope = $scope || $rootScope.$new();

promise = $ionicModal.fromTemplateUrl(tpl, {
scope: $scope,
animation: 'slide-in-right'
}).then(function(modal) {
$scope.modal = modal;
return modal;
});

$scope.openModal = function() {
$scope.modal.show();
};
$scope.closeModal = function() {
$scope.modal.hide();
};
$scope.$on('$destroy', function() {
$scope.modal.remove();
});

return promise;
};

return {
init: init
}

})

然后从 Controller 中,传递 Controller 和模态模板的当前范围

.controller('UserProfileCtrl', function($scope, $state, user, ModalFactory) {    
ModalFactory.init('image-view.html', $scope)
.then(function(modal) {
confirmationModal = modal;
confirmationModal.show();
});
})

希望对您有所帮助。

关于javascript - Ionic - 什么时候应该调用 ionicModal.remove()? [Ionic Modal 隐藏回调],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35933162/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com