Javascript 我想比较angularjs中输入文本框的新旧值是否相同?

Javascript 我想比较angularjs中输入文本框的新旧值是否相同?,javascript,angularjs,html,Javascript,Angularjs,Html,我有两个angularjs html结构的输入框。 当我点击我的按钮或标签时,我希望我的文本框新旧值在angularjs中,或者我想比较新旧值是否相同。 我用的是angular 1.4.7 <input phone-input ng-show="mode == 'edit'" ng-model="leader.work"/> <input phone-input ng-show="mode == 'edit'" ng-model="leader.mobile"/> &l

我有两个angularjs html结构的输入框。 当我点击我的按钮或标签时,我希望我的文本框新旧值在angularjs中,或者我想比较新旧值是否相同。 我用的是angular 1.4.7

<input phone-input ng-show="mode == 'edit'" ng-model="leader.work"/>
<input phone-input ng-show="mode == 'edit'" ng-model="leader.mobile"/>

<a ng-show="mode == 'edit'" ng-click="mode = null;save_profile(leader)" style="cursor: pointer" title="Save">Save</a>

$scope.save_profile = function (leader) {
  /* How to get/compare both text box old and new value are same or not*/
};

拯救
$scope.save_profile=函数(leader){
/*如何获取/比较文本框新旧值是否相同*/
};

您可以使用$watch功能。下面的链接将向您展示如何实现它。您可以使用它获得新旧值


您可以使用$watch功能。下面的链接将向您展示如何实现它。您可以使用它获得新旧值

试试这个

函数TodoCrtl($scope){ $scope.newValue='初始文本'; $scope.save_profile=函数(新值、旧值){ //在这里,您可以从范围访问旧值和新值。 $scope.new='新值:'+$scope.newValue; $scope.old='旧值:'+$scope.oldValue; //访问后,使用函数参数将范围旧值更新为新值 $scope.newValue=newValue; $scope.oldValue=newvalue; }; $scope.changeValue=函数(){ $scope.newValue='dynamicchange'; }; }

ng点击
拯救
{{new}}
{{old}}

变化动态
试试这个

函数TodoCrtl($scope){ $scope.newValue='初始文本'; $scope.save_profile=函数(新值、旧值){ //在这里,您可以从范围访问旧值和新值。 $scope.new='新值:'+$scope.newValue; $scope.old='旧值:'+$scope.oldValue; //访问后,使用函数参数将范围旧值更新为新值 $scope.newValue=newValue; $scope.oldValue=newvalue; }; $scope.changeValue=函数(){ $scope.newValue='dynamicchange'; }; }

ng点击
拯救
{{new}}
{{old}}

变化动态
在任何情况下都有效的最简单方法是在加载时复制
引线
,并将当前的
引线
与按下按钮时所做的副本进行比较

function TodoCtrl($scope) {

  // initialization
  var originalLeader = null;
  $scope.leader = null;
  $scope.mode = 'edit';

  // this is where you get your leader data, in my example 
  // I simply set it to demo data but you can load the
  // data using AJAX for example
  var loadLeader = function() {
    var leaderData = {
      mobile: '000',
      work: '111'
    };
    originalLeader = angular.copy(leaderData);
    $scope.leader = leaderData;
  }

  // loadLeader will be invoked on page load
  loadLeader();

  $scope.save_profile = function (leader) {
    // you have access to your original data and current data,
    // you can compare them and do whatever you want with them
    console.log('originalLeader ', originalLeader);
    console.log('leader ', leader);

    // for example
    if ( leader.mobile != originalLeader.mobile ) {
      alert('Mobile has changed from ' + originalLeader.mobile + ' to ' + leader.mobile);
    }
  };
}
一些答案建议使用
$scope.$watch
,您可以使用它来实现您的解决方案,但您需要小心,因为每次更改都会调用
$scope.$watch
回调。为了说明我的意思,让我们在代码中添加如下内容:

$scope.$watch('leader.mobile', function(newVal,oldVal) {
  console.log('newVal ', newVal);
  console.log('oldVal ', oldVal);
});
leader.mobile
在初始时间为
000

您在文本框中键入
1
,现在为
leader。mobile
0001
,将调用回调函数,日志为:

newVal  0001
oldVal  000
现在,按
backspace
并删除之前键入的
1
,则
leader.mobile
变量现在为
000
,日志为:

newVal  000
oldVal  0001
您的当前数据与起始数据相同,但
$scope.$watch
被调用了两次,很难确定数据是否真的发生了更改。为此,您需要实现一些额外的代码,如下所示:

// be careful about this, you need to set to null if you reload the data
var originalMobile = null;

$scope.$watch('leader.mobile', function(newVal,oldVal) {
  // set originalMobile to value only if this is first 
  // invocation of the watch callback, ie. if originalMobile
  // was not set yet
  if ( originalMobile == null ) {
    originalMobile = oldVal;
  }
});

$scope.save_profile = function(leader) {
  if ( leader.mobile != originalMobile ) {
    // ...
  }
}

在任何情况下都适用的最简单方法是,在加载时复制
引线
,并将当前的
引线
与按下按钮时所做的副本进行比较

function TodoCtrl($scope) {

  // initialization
  var originalLeader = null;
  $scope.leader = null;
  $scope.mode = 'edit';

  // this is where you get your leader data, in my example 
  // I simply set it to demo data but you can load the
  // data using AJAX for example
  var loadLeader = function() {
    var leaderData = {
      mobile: '000',
      work: '111'
    };
    originalLeader = angular.copy(leaderData);
    $scope.leader = leaderData;
  }

  // loadLeader will be invoked on page load
  loadLeader();

  $scope.save_profile = function (leader) {
    // you have access to your original data and current data,
    // you can compare them and do whatever you want with them
    console.log('originalLeader ', originalLeader);
    console.log('leader ', leader);

    // for example
    if ( leader.mobile != originalLeader.mobile ) {
      alert('Mobile has changed from ' + originalLeader.mobile + ' to ' + leader.mobile);
    }
  };
}
一些答案建议使用
$scope.$watch
,您可以使用它来实现您的解决方案,但您需要小心,因为每次更改都会调用
$scope.$watch
回调。为了说明我的意思,让我们在代码中添加如下内容:

$scope.$watch('leader.mobile', function(newVal,oldVal) {
  console.log('newVal ', newVal);
  console.log('oldVal ', oldVal);
});
leader.mobile
在初始时间为
000

您在文本框中键入
1
,现在为
leader。mobile
0001
,将调用回调函数,日志为:

newVal  0001
oldVal  000
现在,按
backspace
并删除之前键入的
1
,则
leader.mobile
变量现在为
000
,日志为:

newVal  000
oldVal  0001
您的当前数据与起始数据相同,但
$scope.$watch
被调用了两次,很难确定数据是否真的发生了更改。为此,您需要实现一些额外的代码,如下所示:

// be careful about this, you need to set to null if you reload the data
var originalMobile = null;

$scope.$watch('leader.mobile', function(newVal,oldVal) {
  // set originalMobile to value only if this is first 
  // invocation of the watch callback, ie. if originalMobile
  // was not set yet
  if ( originalMobile == null ) {
    originalMobile = oldVal;
  }
});

$scope.save_profile = function(leader) {
  if ( leader.mobile != originalMobile ) {
    // ...
  }
}

对于这两个文本框中要执行此操作的文本框,我假设您要将加载时文本框中的原始值与按下按钮时文本框中的最终变量进行比较。我说的对吗?对于这两个文本框中的哪个文本框,您想这样做我假设您想将加载时文本框中的原始值与按下按钮时文本框中的最终变量进行比较。我说的对吗?他想在点击按钮时比较数值。。。您可能应该查看您的答案…您可以使用watch获得新旧值,然后在watcher函数中比较它们。为什么这是错误的?你建议他在
leader.work
leader.mobile
上添加
$scope.$watch
,当他想要比较原始值(而不是中间值)和最终值(当他点击按钮时文本框中的值)时,每次更改都会调用这些值。我再次阅读了问题,将原始值与最终值进行比较是我的假设,因为这在问题中并不清楚,但对我来说这似乎是合理的。他希望在单击按钮时比较值。。。您可能应该查看您的答案…您可以使用watch获得新旧值,然后在watcher函数中比较它们。为什么这是错误的?您建议他在
leader.work
leader.mobile
上添加
$scope.$watch
,当他想要比较原始值(而不是中间值)和最终值(当他点击t时,文本框中的值)时,每次更改都会调用这些值