[英]$scope.$watch whole form, but get old value only once
我想在整个表单输入上使用$ scope。$ watch并检测更改,然后显示警报“数据已更改。请保存”。 问题是我只想从服务器获取数据时才传递oldValue。
$http({
method: "post",
url: "url",
data: {
Pages: {
id: pageId
}
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data) {
$scope.data = data.editedPage.jkOutputContainer.editedPage.pagesObject;
$scope.$watch('data',function(newValue, oldValue) {
if(newValue != oldValue) {
$scope.dataHasChanged = true;
} else {
$scope.dataHasChanged = false;
}
}, true);
});
我可以在表单的每个输入上使用ng-init和ng-change,但是我想在表单上使用$ scope。$ watch。
编辑:总之,当用户将其更改返回到从服务器获取的状态时,我想隐藏警报。
要解决您的问题,请勿将服务器的响应绑定到$scope
变量,而应将其分配给脚本中引用的变量。 这是一个例子。
$http({
method: "post",
url: "url",
data: {
Pages: {
id: pageId
}
},
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
}).success(function(data) {
// To solve your problem, just store the server data in a variable.
var fromServer = data.editedPage.jkOutputContainer.editedPage.pagesObject;
$scope.data = fromServer; //<- Reference the var here.
$scope.$watch('data',function(newValue, oldValue) {
if(newValue != fromServer) { //<- Reference the var here.
$scope.dataHasChanged = true;
} else {
$scope.dataHasChanged = false;
}
}, true);
});
希望对您有所帮助!
$scope.watch('[formname]', checkChanged, true)
function checkChanged(newVal) {
if (!angular.equals(newVal,$scope.data)) warnUser()
}
^这将监视表单中的每个表达式并提醒用户
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.