输入文本框中的角度ng变化与旧值

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)

的jsfiddle

编辑 您在编辑中提到了一项新要求,因此我编辑了我的答案.你不应该使用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)

  • @ naveen23:你需要处理你的写作,这听起来很粗鲁和不值得. (10认同)
  • @Elisa:如果在__helping___ some_stranger_ __ for free__的努力中出现部分误解,证明你的世界是粗鲁和咄咄逼人,我很高兴我们不是朋友.你甚至没有意识到,如果存在误解,尤其是__answerers__浪费时间,__ not__ the askers. - 编辑:OTOH,你的意见在看到你的1/10答案/提问比率后不再让我感到惊讶,或者更具体地说是1/40与实际的upvotes.这一次,似乎是捐赠一些空闲时间的非体验指标.你是[寄生虫](http://en.wikipedia.org/wiki/Parasitism)吗? (3认同)

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)


Sat*_*pal 2

您可以使用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)

普朗克演示