gpt4 book ai didi

AngularJS $destroy 动态插入的组件

转载 作者:行者123 更新时间:2023-12-04 03:20:02 28 4
gpt4 key购买 nike

我正在构建一个提供打开和关闭它的服务的模式。该模式有一个控制关闭按钮的小 Controller ,以及进入模式内容的模板的 $compile

那个模板就是一个组件,当然,那个组件有一个 Controller 。

隐藏模态后如何销毁该组件?从技术上讲,当 ng-if 负责从 DOM 中删除我的模式时,它仍然会加载该组件。

代码如下:

modal.controller.js

class ModalController {
constructor($scope, modalService, $compile, $timeout) {
this.$scope = $scope;
this.modalService = modalService;
this.$compile = $compile;
this.$timeout = $timeout;
}

$onInit() {
this.$scope.$watch(this.modalService.getConfig(), (newVal) => {
this.config = newVal.isDisplayed
? angular.extend(this.config, newVal)
: {};

// I wait for ng-if to put the modal into the DOM then I
// compile the component.
this.$timeout(() => {
if (this.config.template) {
const component = this.$compile(this.config.template)(this.$scope);
angular.element('#modal-content').html(component);
this.config.isRendered = true;
}
}, 0);
}, true);
}

close() {
this.modalService.close();
}
}

ModalController.$inject = [
'$scope',
'modalService',
'$compile',
'$timeout',
];

export default ModalController;

modal.service.js

class ModalService {
constructor($timeout) {
this.config = {};
this.$timeout = $timeout;
}

open(newConfig) {
this.config = newConfig;
this.config.isDisplayed = true;
}

close() {
this.config.template = null;
this.config.isRendered = false;

// the reason there is timeout here is to run my CSS animation
// before ng-if removes the modal from the DOM.
this.$timeout(() => {
this.config.isDisplayed = false;
this.config = {};
}, 310);
}

getConfig() {
return () => this.config;
}
}

ModalService.$inject = [
'$timeout',
];

export default ModalService;

我调用模态的 BarController:

class BarController {
constructor(modalService) {
this.modalService = modalService;
}

openModal() {
this.modalService.open({
title: 'Add a document',
template: '<foo-component></foo-component>',
});
}
}

BarController.$inject = [
'modalService',
];

export default BarController;

最佳答案

啊哈!事实证明,它并没有那么复杂。您只需要存储组件的范围,然后在模态 Controller 的关闭函数中调用 $destroy。

class ModalController {
constructor($scope, modalService, $compile, $timeout) {
this.$scope = $scope;
this.modalService = modalService;
this.$compile = $compile;
this.$timeout = $timeout;
}

$onInit() {
this.childScope = null;

this.$scope.$watch(this.modalService.getConfig(), (newVal) => {
this.config = newVal.isDisplayed
? angular.extend(this.config, newVal)
: {};

this.$timeout(() => {
if (this.config.template) {
this.childScope = this.$scope.$new();

angular
.element('#modal-content')
.html(this.$compile(this.config.template)(this.childScope));

this.config.isRendered = true;
}
}, 0);
}, true);
}

close() {
this.config.isRendered = false;

// this timout is used to make sure the CSS transition is executed
// before the component is removed from the DOM.
this.$timeout(() => {
this.childScope.$destroy();
angular.element('#modal-content').empty();
this.modalService.close();
}, 310);
}
}

ModalController.$inject = [
'$scope',
'modalService',
'$compile',
'$timeout',
];

export default ModalController;

关于AngularJS $destroy 动态插入的组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38899959/

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