访问Aurelia中的DOM元素 [英] Access a DOM element in Aurelia

查看:92
本文介绍了访问Aurelia中的DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将如何访问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活动()上的导航。导航位于另一个视图模型/模板对中,所以范围可能被认为是全局的。

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>

viewModel

class ViewModel { 

    canDeactivate() {
        var form = this.myForm;
        // do stuffs
    }
}

有关更多信息在ref属性上,请参阅: http://aurelia.io /hub.html#/doc/article/aurelia/framework/latest/cheat-sheet/5

For more information on the ref attribute, see here: http://aurelia.io/hub.html#/doc/article/aurelia/framework/latest/cheat-sheet/5

这篇关于访问Aurelia中的DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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