ng-change仅适用于退格和首次进入

时间:2018-06-30 08:23:54

标签: html angularjs

<input type="email" ng-keypress="testFunc()" ng-model="text"
    id="femail" class="form-control margin" name="femail"
    placeholder="Email*"/>
<p style="color: red;">The input field has changed {{count}} times.</p>

JS

$scope.count = 0;
$scope.testFunc = function() {
    $scope.count++;
};

HTML

<input type="email" ng-change="testFunc()" ng-model="text"
       id="femail" class="form-control margin" name="femail"
       placeholder="Email*"/> 
<p style="color: red;">The input field has changed {{count}} times.</p>

JS

$scope.count = 0;
$scope.testFunc = function() {
    $scope.count++;
};

如果我输入:“ abc”。结果应该是:3,但是我得到了:0,如果我使用backspack删除,我得到了:1我应该在没有得到的每个输入上得到count ++。我正在尝试以下示例:

  

https://w3schools.com/angular/tryit.asp?filename=try_ng_ng-change

在我的代码中。谢谢

说明: 在上面提供的 w3school 示例中,我的代码区别是我在使用

<input type="email"> 

而w3正在使用

<input type="text">

因此,对于“文本输入”,每按一次按钮都被视为一种更改,但是在我的情况下,每一次更改都发生在电子邮件更改或具有

的ng-change功能的某些不同功能时
"input type="email".

所以我现在通过

进行计数
<input type="text" />

2 个答案:

答案 0 :(得分:1)

我只是将示例从W3(您提供的)复制/粘贴到test.txt文件,并将其重命名为test.html并在Firefox中运行。一切都很好。我不必编辑任何内容。先尝试我做过的事情,然后您就会知道是错字还是其他。但是代码正常工作。

答案 1 :(得分:0)

您可以使用

<input type="text" /> 

对于您的计数器来说,这是更好的解决方案。 如果您想要柜台,请使用 type =“ text”而不是“ email” 。浏览器将它们区别对待。