- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们一直在项目中使用 @ng-bootstrap/ng-bootstrap 库来实现某些行为/组件,例如 Modal。最近我想消除这种依赖性并使用 Angular 实现引导模式行为。这实际上很容易。让我简短地告诉您它是如何工作的:
我有一个模态服务和一个模态组件。 Service 通过 ComponentFactoryResolver 动态创建模态组件(详情见 in this SO post )并添加到 DOM 中。通过关闭模态,模态只是调用从服务定义的回调函数,这只是销毁组件,从 DOM 中删除。
所以:我这个模态组件有 2 个动画状态,进入和离开。输入效果很好。一旦组件出现在 dom 中,预定义的 :enter 状态就会被触发,我的动画就会开始工作。但是 :leave 却没有。
这正是关闭模态框的工作原理:模态框打开,您单击关闭按钮或模态背景上的任何其他位置。这只是调用 close 函数,该函数被定义为输入,并在创建期间从服务给出。
@Input() closeCallback: Function;
服务只是从 DOM 中删除组件。
由于单击关闭按钮后组件就会被删除,因此我认为动画没有所需的时间。所以 :leave 不起作用。
我想在关闭时设置超时(延迟),并手动触发动画,但由于我想使用预定义的行为:enter和:leave,我无法弄清楚它是如何实现的。那么我怎样才能让我的离开动画发挥作用呢? (有或没有:leave)
服务代码:
@Injectable()
export class ModalService implements OnDestroy {
private renderer: Renderer2;
private componentRef: ComponentRef<ModalComponent>;
constructor(private rendererFactory: RendererFactory2,
private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector) {
this.renderer = rendererFactory.createRenderer(null, null);
}
ngOnDestroy() {
this.componentRef.destroy();
}
open(content: string, titel: string, primaryButtonLabel: string, secondaryButtonLabel?: string, primaryButtonCallback?: Function, secondaryButtonCallback?: Function) {
// 1. Create a component reference from the component
this.componentRef = this.componentFactoryResolver
.resolveComponentFactory(ModalComponent)
.create(this.injector);
this.componentRef.instance.content = content;
this.componentRef.instance.titel = titel;
this.componentRef.instance.primaryButtonLabel = primaryButtonLabel;
this.componentRef.instance.secondaryButtonLabel = secondaryButtonLabel;
this.componentRef.instance.primaryButtonCallback = primaryButtonCallback;
this.componentRef.instance.secondaryButtonCallback = secondaryButtonCallback;
this.componentRef.instance.closeCallback = (() => {
this.close();
});
// 2. Attach component to the appRef so that it's inside the ng component tree
this.appRef.attachView(this.componentRef.hostView);
// 3. Get DOM element from component
const domElem = (this.componentRef.hostView as EmbeddedViewRef<any>)
.rootNodes[0] as HTMLElement;
// 4. Append DOM element to the body
this.renderer.appendChild(document.body, domElem);
this.renderer.addClass(document.body, 'modal-open');
}
close() {
this.renderer.removeClass(document.body, 'modal-open');
this.appRef.detachView(this.componentRef.hostView);
this.componentRef.destroy();
}
}
模态组件.ts:
@Component({
selector: '[modal]',
templateUrl: './modal.component.html',
styleUrls: ['./modal.component.scss'],
animations: [
trigger('modalSlideInOut', [
transition(':enter', [
style({opacity: 0, transform: 'translateY(-100%)'}),
animate('0.3s ease-in', style({'opacity': '1', transform: 'translateY(0%)'}))
]) ,
transition(':leave', [
style({opacity: 1, transform: 'translateY(0%)'}),
animate('0.3s ease-out', style({'opacity': '0', transform: 'translateY(-100%)'}))
])
])
]
})
export class ModalComponent implements AfterViewInit {
....
@Input() closeCallback: Function;
constructor() { }
close() {
this.closeCallback();
}
}
Modal-HTML 不是很相关,但你可以像这样想象:
<div [@modalSlideInOut] role="document" class="modal-dialog">
<div ....
<button (click)="close()">
CLOSE
</button>
...
</div>
</div>
最佳答案
我今天遇到了类似的问题,我找到的解决方案是简单地将动画绑定(bind)到 Host 组件本身:
@HostBinding('@modalSlideInOut')
这样,您就不必对动画计时进行任何欺骗。当您调用 destroy 时,Angular 知道组件即将消失,因此它会为您处理它,就像您在组件上调用 ngIf 一样。
关于Angular 5 动画 : How to work with :leave transition while the component is removed from DOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51589893/
LOOP 一词被描述为“解析所有未解析的 LEAVE 事件的目的地”。 (强调我的) 与 IF ... ELSE ... THEN 的前向引用数始终为 1 不同,LOOP 对 LEAVE 的数量没有限
我正在尝试实现 Kd 树以在 C++ 中执行最近邻和近似最近邻搜索。到目前为止,我遇到了最基本的 Kd 树的 2 个版本。 一种,数据存储在节点和叶子中,例如 here 一种,数据只存储在叶子中,例如
我没有清楚地理解 LEAVE 函数,它是这两条指令的浓缩: MOV ESP, EBP POP EBP 因此,MOV ESP, EBP 将 ESP 向下移动到 EBP 的级别(堆栈的开头)。 然后POP
我有外部路线:/posts 和 /about。 /posts 路由具有嵌套路由:/ 和 /pages/:pageNumber。 在嵌套路由(/ 和 /pages/:pageNumber)之间导航时,动
我正在尝试在 JavaFX 中设置一个复选框树,其中叶子绑定(bind)到其父级: 当选择父级时,它们也会被选择 无法单独更改。 我使用与 selectedProperty 的绑定(bind)来完成此
程序如下: main:BEGIN # make sure a game isnt already running SELECT COUNT(*), `winner` INTO @running, @w
使用什么模式将字符保留在字符串中的点之间“12395.KS_TH014.001239”,所以如果其动态变量,则只剩下“KS_TH014”。我尝试了模式“.\K.*?(?=.)”,但它在 mysql 中
我有如下例子中的触发器: BEFORE UPDATE ON icharts_user_stats FOR EACH ROW BEGIN IF (NEW.col1> OLD.col1 OR NE
我正在为 iOS 编写一个应用程序来阅读 PDF 文档并使用“Leaves”库。除了宽度为 1px 的白线外,一切都很酷,它会在您翻过页面时出现。有没有人遇到过这个?你是如何解决这个问题的? 最佳答案
我写了以下函数 window.onbeforeunload = function(){ return 'Are you sure you want to leave?'; }; 现在我希望当用户单
我的网站上有以下 href: a href="http://www.google.com" name="external" onclick="confirmExit(this.name) 如果用户不想
我只看到了另一个类似的问题 ( this one ),但我不知道如何将其应用到我的页面。我是网络开发新手,所以我使用了 this template索取联系表格。您可以在这里查看我的联系页面:http:
我正在为我的元素使用 animate.css 和 Angular.JS。我创建了一个代码笔,您可以在这里找到它:http://codepen.io/SassyCrafter/pen/NrRbmE .以
我在几个地方都见过它,例如Eclipse XML 自动格式化也可以做到这一点:在空元素的结束 /> 之前放置一个空格。 拥有 将被重新格式化为 你为什么要这样做?从 XML 的角度来看,它是等价的
题目地址:https://leetcode.com/problems/sum-of-left-leaves/open in new window Difficulty: Easy 题目大
我正在尝试反汇编一个包含简单功能的简单程序。这个程序是用 gcc 编译的,用于 32 位 x86 目标。 该函数通过调用指令调用。 在函数的最后,我看到一个ret指令,这是正常的,但也有一个leave
我在这里学习 Docker 教程 https://docs.docker.com/get-started/part3/ 当我在页面教程快结束时执行命令 docker swarm leave --for
这个问题已经有答案了: How to show a dialog to confirm that the user wishes to exit an Android Activity? (11 个回
因此,我编写了这一小段代码来尝试一些新的方法来进行图像交换以实现预加载,但我遇到了一些麻烦。 我的问题是,我有一个包含图像的容器,其中有一些填充和文本,但只有当有人滚动图像而不是容器时,才会激活翻转。
我在div上使用hover()来在用户将鼠标移到上方时显示信息面板,这在90%的情况下有效,但有时事件似乎无法正确触发。这似乎与您将鼠标移入或移出 div 的速度有关。 我设置了一个 jsfiddle
我是一名优秀的程序员,十分优秀!