我发现自己经常在多个控制器中重复以下代码,每个业务对象一个:
$scope.original = {name:"John"};
$scope.status = {error:false};
$scope.editMode = false;
$scope.cancel = function() {
$scope.item = angular.copy($scope.original);
$scope.editMode = false;
};
$scope.edit = function() {
$scope.editMode = true;
};
$scope.item = angular.copy($scope.original);
$scope.save = function(item) {
// do something ajax here
// if it comes back 409, mark as conflict
if ($scope.status.error) {
$scope.myForm.name.$setValidity("conflict",false);
} else {
$scope.original = angular.copy($scope.item);
$scope.editMode = false;
}
};
问题有几个:
$scope.myForm
可以访问$scope
,特别是因为其他指令可能在表单或输入元素周围创建了隔离的范围resource.$save()
或其他$http
活动,但感觉这种类型的直接有效性设置属于指令。