从控制的角度范围内的DOM元素 [英] The dom element in context of view from the controller

查看:129
本文介绍了从控制的角度范围内的DOM元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个角视图的形式。而在窗体上的按钮,我已经添加了一个事件,发送请求使用的$ HTTP服务器。发送请求到服务器之前,我需要使用jQuery验证来验证表单。

I have a form in a angular view. And in the button on the form, I've added an event to send request to the server using $http. Before sending the request to the server, I need to validate the form using jQuery Validate.

要验证形式,我需要的形式的控制器中的DOM参考。我已经有jQuery的在我的应用程序。因此,可以通过获取表单的参考

To validate the form, I need the dom reference of the form in the controller. I already have jQuery in my application. So can get the reference of the form using

$(selecter) or angular.element($(selecter))

但是,在这两种情况下,它会查询的完整的文档来获得元件的参考。代替的,我需要的观点的情况下,这将增加性能内查询的元素

But in both cases, it'll search the full document to get the reference of the element. Instead of that I need to search the element within the context of the view, which will increase the performance.

我见过的角度利用$元素。但后来我看到是不推荐使用$元素,它可能是在角新版pciated德$ P $。当我试图让控制器内$元素,我发现了一个错误。所以我想这是德$ P $已经pciated。

I've seen the use of $element in angular. But later I've seen that the use of $element is not recommended and it may be depreciated in the new version of angular. And when I'm trying to get $element within the controller, I'm getting an error. So I guess it's depreciated already.

那么什么是从控制器获得的视图上下文的DOM元素的最佳方式?

So what is the best way to get dom element in context of view from the controller?

推荐答案

究竟是米卡莱说。与您必须创建指令的任何元素处理(这是角度的方式..)

Exactly as Mikalai said. To manipulate with any element you have to create directive(this is angular way..)

有关的例子:


  • HTML <输入类型=文本全选值=测试/>

  • 指令

  • html <input type="text" select-all value="test" />
  • directive

app.directive('selectAll', function () {
return {
    restrict: 'A',
    link: function(scope, element, attrs) {
        element.on('click', function() {
            this.select();
        });
    };
});


这只是指令中选择文本元素中的所有文本。

This directive just select all text in text element.

更新:

我创建 plunker 你。这是你问什么?

I've created plunker for you. Is this what you're asking for?

更新2

我才意识到,我们并不需要的eval这里的。我不知道,为什么我把它放在那里摆在首位。 EVAL也可能是有害的。

I just realized, that we don't need eval here at all. I don't know, why I put it there in the first place. Eval could also be harmful.

看看这个 plunker

这篇关于从控制的角度范围内的DOM元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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