AngularJS,具有清晰的按钮输入文本 [英] AngularJS, clear input text with button

查看:130
本文介绍了AngularJS,具有清晰的按钮输入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 AngularJS ,什么是清除文本字段的值的正确方法?我有一个输入字段旁边的按钮。它的用户类型并点击了按钮,以清除或重置。

我知道我可以在按钮本身添加一个 NG-点击事件并调用一个函数,但我不知道那是做的正确方法。

现在我所拥有的是:

 <输入类型=文本NG模型=SEARCHQUERY/>
<按钮类=BTN<! - 做一些事情,也许在这里 - >>
  < I类=图标搜索NG-CLASS ={图标刷新:searchQuery.length}>< / I>
< /按钮>


解决方案

NG-点击是角的方式来做到这一点。

只是声明一个函数在你的范围,将其连接到按钮的NG-点击

如果你想要的东西更先进,那么角办法提出建立指令,以包你想它的功能并以某种方式连接到您的按钮(这取决于你如何执行你的指令)

如果你想要一个例子,让我知道在评论

Using AngularJS, what is the proper way to clear the value of a text field? I have an input field with a button next to it. The user types in it and hits the button to clear or reset it.

I know I can add an ng-click event on the button itself and call a function, but I am not sure that is the correct way to do it.

Right now all I have is:

<input type="text" ng-model="searchQuery" />
<button class="btn" <!--do something here maybe?-->>
  <i class="icon-search" ng-class="{'icon-refresh': searchQuery.length}"></i>
</button>

解决方案

ng-click is the "Angular" way to do it

just declare a function in your scope and attach it to the button's ng-click

If you wanted something more advanced, then the "Angular way" suggests creating a directive to wrap the functionality you want in it and attach it somehow to your button (depending on how you implement your directive)

If you want an example, let me know in the comments

这篇关于AngularJS,具有清晰的按钮输入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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