访问 Aurelia 中的 DOM 元素 [英] Access a DOM element in Aurelia
问题描述
您将如何访问 Aurelia 中的 DOM 元素?这是一个广泛而笼统的问题,但我觉得有一种或两种首选方法可以做到这一点.我现在在 Aurelia 有两个案例:
How would you go about accessing a DOM element in Aurelia? This is a broad and general question, but I have a feeling there are one or two preferred ways to do this. I have two current cases in Aurelia now:
在模板中我有一个表格.我想在 VM canDeactivate() 上访问视图模型中的表单元素,以中断用户导航离开已填写一半的表单.所以我试图访问元素的范围可以被认为是本地的.
In the template I have a form. I want to access the form element in the view-model, on VM canDeactivate(), to interrupt a user navigating away from a half filled out form. So the scope in which I'm trying to access the element can be considered local.
在另一个视图模型中,我想隐藏 VM activate() 上的导航.导航驻留在另一个视图-模型/模板对中,因此范围可能被视为全局.
In another view-model I want to hide navigation on VM activate(). Navigation resides in another view-model/template pair so the scope may be considered global.
推荐答案
按照 Rob 的建议,使用 ref
.例如:
As Rob suggested, use ref
. For your example:
查看
<form ref="myForm"></form>
视图模型
class ViewModel {
canDeactivate() {
var form = this.myForm;
// do stuffs
}
}
有关 ref 属性的更多信息,请参见此处:http://aurelia.io/docs/binding/basics#function-references
For more information on the ref attribute, see here: http://aurelia.io/docs/binding/basics#function-references
这篇关于访问 Aurelia 中的 DOM 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!