[英]Can't clear input field in Angular on click
I am stuck and I have no clue what I am doing wrong here. 我被困住了,我不知道我在做什么错。 So this is the HTML I am using:
这就是我正在使用的HTML:
<ion-searchbar class="searchBar">
<label>
<input class="searchField" type="search" placeholder="Search" ng-model="findAndSearch" />
<a class="clear" ng-click="findAndSearch = '' ">X</a>
<i class="icon ion-search placeholder-icon"></i>
</label>
</ion-searchbar>
And this is the output HTML when I have typed / typing something into my input field: 这是当我在输入字段中键入内容/输入内容时的输出HTML:
<ion-searchbar class="searchBar">
<label>
<input class="searchField ng-valid ng-not-empty ng-dirty ng-valid-parse ng-touched" type="search" placeholder="Search" ng-model="findAndSearch" />
<a class="clear" ng-click="findAndSearch = '' ">X</a>
<i class="icon ion-search placeholder-icon"></i>
</label>
</ion-searchbar>
So what does this say? 那么这是什么意思呢? It says that it automatically puts the classname
ng-not-empty
and ng-not-empty
when it's respectively being not empty and being empty (not filled in). 它说,当类名分别为不为空和为空(未填写)时,它将自动放入
ng-not-empty
和ng-not-empty
。
Whenever I click on the button I've made, it doesn't do a thing. 每当我点击我创建的按钮时,它都无济于事。 I have no errors in my console, but as I said: whenever I write something into my input field, I can't reset it / remove it with this button.
我的控制台没有错误,但是正如我所说:每当我在输入字段中写入内容时,都无法使用此按钮重置/删除它。
Any idea what I am doing wrong or what might be going wrong? 知道我做错了什么或可能出了什么问题吗?
PS: I already tried every possible solution in this SO post . PS:我已经在本文中尝试了所有可能的解决方案。
I've got something from Angular Documentation. 我从Angular文档中得到了一些东西。
It's realted to a button, but I don't think this is a problem. 它位于按钮上,但我认为这不是问题。
Check this out: Clear Model 检查一下: 清除模型
The next example shows how to debounce model changes.
下一个示例显示如何消除模型更改。 Model will be updated only 1 sec after last change.
上次更改后仅1秒钟将更新模型。 If the Clear button is pressed, any debounced action is canceled and the value becomes empty.
如果按下清除按钮,则任何反跳动作都会被取消,并且该值将变为空。
<div ng-controller="ExampleController">
<form name="userForm">
Name:
<input type="text" name="userName"
ng-model="user.name"
ng-model-options="{ debounce: 1000 }" />
<button ng-click="userForm.userName.$rollbackViewValue(); user.name=''">Clear</button><br />
</form>
<pre>user.name = <span ng-bind="user.name"></span></pre>
</div>
Not the debounce, but the $rollbackViewValue() to specific field in your form. 不是去抖,而是$ rollbackViewValue()到表单中的特定字段。 And after user.name=''
并且在user.name =''之后
Please, try and tell us. 请尝试告诉我们。
In your code: 在您的代码中:
<a class="clear" ng-click="findAndSearch = '' ">X</a>
You try: 你试试:
<a class="clear" ng-click="yourForm.(give some ID to your input).$rollbackViewValue(); findAndSearch = ''">X</a>
Okay, so I have fixed this problem and I still don't know why or what is causing it, but the fix is like this: 好的,所以我已经解决了这个问题,但我仍然不知道为什么或是什么原因,但是解决方法是这样的:
<ion-searchbar class="searchBar">
<label>
<input class="searchField" type="search" placeholder="Search" ng-model="findAndSearch" />
<i class="icon ion-search placeholder-icon"></i>
</label>
<a class="clear" ng-click="findAndSearch = '' ">X</a>
</ion-searchbar>
As you can see, I have put the anchor with the ng-click
outside of the label
element and now, suddenly, it works. 如您所见,我将
ng-click
锚定放置在label
元素之外,现在,它突然起作用了。 Unbelievable. 难以置信的。
Thanks for the help guys! 感谢您的帮助!
I have created snippet on jsfiddle with your code and all work fine: ngModel is cleared and class ng-not-empty removed when you click on X. So looks like the problem not in this piece of code 我已经使用您的代码在jsfiddle上创建了代码段,并且一切正常:当您单击X时,ngModel被清除并且ng-not-empty类被删除。所以看起来问题不在此段代码中
<div ng-app="app">
<ion-searchbar class="searchBar">
<label>
<input class="searchField" type="search" placeholder="Search" ng-model="findAndSearch" />
<a class="clear" ng-click="findAndSearch = '' ">X</a>
<i class="icon ion-search placeholder-icon"></i>
</label>
</ion-searchbar>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.