admin管理员组文章数量:1194371
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>
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>
Share
Improve this question
edited Mar 9, 2013 at 0:51
Ronnie
asked Mar 9, 2013 at 0:39
RonnieRonnie
11.2k22 gold badges84 silver badges142 bronze badges
2 Answers
Reset to default 30I had the same problem. Maybe simple is best?
No need for calling a method in the controller. No need for a directive either. Just a brief statement to clear the model that you declared in the input tag. Like this:
<input ng-model="searchQuery">
<button class="btn" ng-click="searchQuery = ''"></button>
You may also hide the clear button when there is no text in the input, like this:
<button class="btn" ng-show="searchQuery.length" ...></button>
By the way, maybe a directive that would put the clear button inside of the input box would be nice. Something like you see on the iPhone.
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
本文标签: javascriptAngularJSclear input text with buttonStack Overflow
版权声明:本文标题:javascript - AngularJS, clear input text with button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738109953a2064500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论