当输入字段被修改时,Angular会将“ng-dirty”类添加到输入字段,而不管是否newValue==oldValue
。
即。如果输入值是'manohar',我删除'r'并将其添加回来。即使值为'manohar'(原始值),'ng-dirty'类仍然存在。
有没有办法检查输入值是否被修改(newValue != oldValue
)?
提前致谢。
答案 0 :(得分:3)
如果您想根据表单中的数据实际状态(编辑与原始状态)处理行为,您需要自己跟踪。像Cétiasais一样,脏/ prestine只是为了跟踪交互和验证,而不是状态。
这可能有点矫枉过正,但我通常做这样的事情:
.controller('MyController', function ($scope, Restangular) {
/**
* @ngdoc property
* @name _dataWatcher
* @private
* @propertyOf MyController
*
* @description
* Holds the watch stop-method, if the "data-has-changed" watcher is active.
*/
var _dataWatcher;
/**
* @ngdoc property
* @name data
* @propertyOf MyController
*
* @description Stores "original" and "edit" copy of the data beeing handled by the form.
*/
$scope.data = {};
/**
* @ngdoc property
* @name data
* @propertyOf MyController
*
* @description Stores state for the view.
*/
$scope.state = {
loading: true,
saving: false,
hasChanged: false,
hasBeenSaved: false
};
/**
* @ngdoc method
* @name _setup
* @private
* @methodOf MyController
*
* @description Run on controller init.
*/
function _setup() {
$scope.reset();
$scope.state.loading = false;
};
/**
* @ngdoc method
* @name _startWatcher
* @private
* @methodOf MyController
*
* @description
* Setup watcher for changes in data.test. Stop method will be saved to
* private property _dataWatcher.
*/
function _startWatcher() {
_dataWatcher = $scope.$watch('data.edit', function (newValue, oldValue) {
if (newValue !== oldValue) {
if (JSON.stringify(newValue) === JSON.stringify($scope.data.original)) {
$scope.state.hasChanged = false;
// If you want the form to act like its untouched if no changes are found:
$scope.nameOfYourForm.$setPristine();
} else {
$scope.state.hasChanged = true;
}
}
}, true);
}
/**
* @ngdoc method
* @name _stopWatcher
* @private
* @methodOf MyController
*
* @description
* Stop watching data.test for changes. This is needed for "reset" and
* syncing data without triggering data-has-changed loop.
*/
function _stopWatcher() {
if (!_dataWatcher) {
return;
}
_dataWatcher();
}
/**
* @ngdoc method
* @name save
* @methodOf MyController
*
* @description
* Save data.edit changes to database, Restangular is used in example but you get the idea.
*/
$scope.save = function () {
var newData;
// Set states
$scope.state.error = false;
$scope.state.saving = true;
// Update with new data
// $scope.entity is a restangularized object from some server
newData = Restangular.copy($scope.entity);
newData.name = $scope.data.edit.name;
newData.status = $scope.data.edit.status;
// Save to db
newData.put().then(function (entity) {
_stopWatcher();
// Db returns saved data, update restangular object, to update
// data in view.
$scope.entity.name = entity.name;
$scope.entity.status = entity.status;
$scope.reset();
$scope.state.hasBeenSaved = true;
}, function (error) {
$scope.state.hasChanged = true;
$scope.state.error = error.data;
}).finally(function () {
$scope.state.saving = false;
});
};
/**
* @ngdoc method
* @name reset
* @methodOf MyController
*
* @description
* Resets the data object to current state of the original source
* object. Any unsaved changes in data.edit will be overwritten.
*/
$scope.reset = function () {
// Stop watcher, if initialized
_stopWatcher();
// Save original data (for comparison)
$scope.data.original = {
status: $scope.feed.status,
name: $scope.feed.name
};
// Create new copy of editable data
$scope.data.edit = angular.copy($scope.data.original);
// Make sure state signals "not changed"
$scope.state.hasChanged = false;
// Force form to be prestine
$scope.nameOfYourForm.$setPristine();
// Start watching changes
_startWatcher();
};
_setup();
});
答案 1 :(得分:0)
ngDirty
/ ngPristine
目标是告诉您用户是否修改了表单而不管值是什么。所以在你的情况下,这种行为是完全正常的。
如果要修改此行为,可以使用model.$setPristine()
功能。更多详情:https://code.angularjs.org/1.2.26/docs/api/ng/type/ngModel.NgModelController
答案 2 :(得分:0)
您可能对此项目感兴趣,名为:
它修改$ prisitine以便能够执行以下操作: