shr*_*hrw 12 textbox angularjs angularjs-ng-change
<input type="text" id="exampleab"
ng-model="a.b"
ui-event="{ blur : 'callScriptThenServer()' }" >
Run Code Online (Sandbox Code Playgroud)
由于某些原因,ng-changeon文本框不起作用,所以我正在使用它; 使用Angular-ui ui-events.
我想只在值改变时调用该函数,并且还希望在回调中使用旧值.(因为我想将oldValue发送到服务器).
我不想通过纯指令路由,因为有这么多的出现
NG-CHANGE:每个角色都改变了,我得到一个回调.我不希望这样.我需要调用服务器脚本..使用文本框中的旧值和模糊后的新值
Alb*_*orz 16
您可以观察变量以同时拥有newValue和oldValue.
<input type="text" id="exampleab" ng-model="a.b" >
Run Code Online (Sandbox Code Playgroud)
在你的控制器中:
app.controller('ctrl', function ($scope) {
$scope.$watch('a.b', function (newValue, oldValue) {
console.log('oldValue=' + oldValue);
console.log('newValue=' + newValue);
//do something
});
});
Run Code Online (Sandbox Code Playgroud)
编辑 您在编辑中提到了一项新要求,因此我编辑了我的答案.你不应该使用ng-change.当控件被聚焦时你应该得到oldValue并将其保存在变量中,然后在blur事件上获得新值. 我设立了一个新的小提琴.
在你的控制器中:
app.controller('ctrl', function ($scope) {
$scope.showValues = function () {
alert('oldValue = ' + $scope.oldValue);
alert('newValue = ' + $scope.a.b);
}
});
Run Code Online (Sandbox Code Playgroud)
我是你的看法
<input type="text" id="exampleab" ng-model="a.b" ng-init="oldValue = ''" ng-focus="oldValue = a.b" ng-blur="showValues()" />{{a.b}}
Run Code Online (Sandbox Code Playgroud)
Nie*_*eek 10
使用ng-model-options
<input type="text" ng-model="a.b" ng-change="callScriptThenServer()" ng-model-options="{updateOn: 'blur'}"/>
Run Code Online (Sandbox Code Playgroud)
您可以使用AngularJS 手表
$scope.$watch(function(){
return $scope.a.b;
}, function(newvalue, oldvalue){
//Here You have both newvalue & oldvalue
alert("newvalue: " + newvalue);
alert("oldvalue: " + oldvalue);
},true);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78221 次 |
| 最近记录: |