角度 - 可重用的对话框 [英] angular - reusable dialogs

查看:29
本文介绍了角度 - 可重用的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个对话框以用于项目列表中的任何项目.除了明显依赖于项目的字段值之外,无论项目如何,对话框都几乎相同.

I need to build a dialog to be used with any item on a list of items. The dialog is pretty much the same regardless of the item except for the values of the fields which are obviously item dependent.

我正在构建的指令是从文件中读取模板,用 $compile 编译它,然后将它绑定(链接)到项目的范围.绑定的结果是一棵 DOM 树.为了使对话框可见,我需要将此树附加到现有 DOM 中的某个元素.我的对话框的性质是将它直接附加到 body 标签是有意义的.该对话框将与列表中的不同项目组合使用多次

The directive I am building is reading the template from a file, compiles it with $compile and then binds (links) it to the scope of the item. The result of the binding is a DOM tree. To make the dialog visible I need to append this tree to some element in the existing DOM. The nature of my dialog is such that it makes sense to append it directly to the body tag. The dialog will be used many times in combination with different items on the list

所以我的问题是:这个过程(编译、绑定、追加)有多少可以提前完成?我当然可以运行一次编译.我还可以将编译结果绑定到 $rootscope 并将其附加(隐藏)到 body 标记中.这样我以后就可以打开可见性并显示对话框.

So here is my question: How much of this process (compile, bind, append) can be done in advance? I certainly can run compile once. I can also bind the compilation result to the $rootscope and append (hidden) it to the body tag. This way I can later just turn on visibility and show the dialog.

但是,如果它已经绑定并附加到 DOM,那么将它重新绑定到其他范围是否符合犹太教规?如果是的话 - 正确的方法是什么?另一个问题是它甚至值得吗?可能只是在每次需要时重新插入它?

But if it is already bound and attached to DOM, is it kosher to re-bind it to some other scope, if so - what's the right way to do it? Another question is is it even worth it? might be just re-insert it every time it is needed?

推荐答案

我认为很好的问题.您想知道是否可以热交换"元素的范围.我不知道是否有办法做到这一点,或者即使有,如果那是 Angular 的方式.我认为你是如何看待 ng-view 能达到你所达到的程度吗?

Excellent question I think. You're wondering if one can "hot swap" the scope of an element. I don't know if there's a way to do that, or even if there is, if that's the Angular way. I take it you looked at how ng-view works to get as far as you've gotten?

我的建议是执行 $compile 一次,将结果、链接或嵌入函数或它在 Angular 术语中称为的任何内容保留在某处.并为每个需要的对话框实例调用它.

My advice is to do the $compile once, keep the result, the link or transclusion function or whatever it's called in Angular parlance, around somewhere. And call it for each needed instance of the dialog.

这篇关于角度 - 可重用的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆