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

标签 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"/>

<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*/
};

最佳答案

试试这个

function TodoCrtl($scope) {

  $scope.newValue = 'Initial Text';
  $scope.save_profile = function(newvalue, oldvalue) {

    //Here you can access old value and new value from scope.
    $scope.new = 'New Value :' + $scope.newValue;
    $scope.old = 'Old Value :' + $scope.oldValue;

    //After accessing update the scope old value to new value with function parameters
    $scope.newValue = newvalue;
    $scope.oldValue = newvalue;

  };

  $scope.changeValue = function() {

    $scope.newValue = 'Dynamic Change';
  };

}
<!DOCTYPE html>
<html ng-app>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js"></script>
  <meta charset=utf-8 />
  <title>ng-click</title>
</head>

<body>

  <div ng-controller="TodoCrtl">

    <input type=text ng-model="newValue" ng-init="oldValue=newValue">
    <button ng-click="save_profile(newValue,oldValue)">Save</button>
    <div>{{new}}</div>
    <div>{{old}}</div>

    <br>
    <button ng-click="changeValue()">Change Dynamic</button>

  </div>

</body>

</html>

关于javascript - 我想比较angularjs中输入文本框的旧值和新值是否相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36100084/

相关文章:

javascript - Express 服务器 - 无法发布/

从其他对象属性计算(动态)的 Javascript 对象属性

javascript - ng-repeat 中的 Angular js 动态绑定(bind)

javascript - 如何在 ng-repeater 内部过滤由自定义过滤器过滤的内容

javascript django html 标签

javascript - PHP - 从文本区域中删除换行符

javascript - 未捕获错误 : Syntax error, 无法识别的表达式::nth-child for simple slide

javascript - angular.js如何解析 View 内的变量数据

javascript - 在 Angular js中获取index.php内容

html - CSS:允许图像从div溢出?