简体   繁体   English

使用基于表单和基于非表单的属性来监视对象中的更改

[英]Watch for changes in object with form based and non-form based properties

On a rather big page a user can edit data in various ways: - Change some "classical" input fields - Add files via clicking on a button - Change the order of items via drag and drop 在相当大的页面上,用户可以通过多种方式编辑数据:-更改某些“经典”输入字段-通过单击按钮添加文件-通过拖放更改项目顺序

In the HTML, a simplified example might look like such: 在HTML中,一个简化的示例可能如下所示:

<form>
    Name: <input ng-model="person.name">
    Title: <input ng-model="person.title">
    Image: <our-custom-image-uploade-directive ng-model="person.image"/>
    Skills: <our-custom-skill-drag-and-drop-directive ng-model="person.skills"/>
    <button ng-click="save()">Save</button>
</form>

You see, some edit "facilities" are form-based, some are not. 您会看到,有些编辑“设施”是基于表单的,有些则不是。

Now, there is a rather simple task to do: Disable the "save" button if the user didn't change anything or happened to end up with the very same state of data that it was before the user interacted with the data. 现在,有一个相当简单的任务要做:如果用户未进行任何更改或碰巧遇到的数据状态与用户与数据进行交互之前的状态完全相同,则禁用“保存”按钮。

Now I'm wondering which is the best way to achieve that. 现在我想知道哪种方法是最好的方法。

One way might be to deep watch the whole person object. 一种方法可能是深入观察整个人对象。 Like that: 像那样:

$scope.backupCopyOfPerson = angular.copy($scope.person); // Creae a backup copy of the state before the user changed something

$scope.$watch('person', function (newValue) {
            if(newValue && $scope.backupCopyOfPerson) {
                if(angular.equals(newValue, $scope.backupCopyOfPerson)) {
                    $scope.unsavedChanges = false;

                }
                else {
                    $scope.unsavedChanges = true;
                }
            }
        }, true);

However, deep watching a big object with a lot of sub-objects etc. might cause some serious performance issues. 但是,深入观察具有许多子对象等的大对象可能会导致一些严重的性能问题。

Another idea is, using ng-pristine for the vanilla form fields and do in all other directives etc. $setDirty() / $setPristine() . 另一个想法是, ng-pristine表单字段使用ng-pristine并在所有其他指令中执行$setDirty() / $setPristine() That might be faster, but it's definitely not an elegant solution. 那可能更快,但是绝对不是一个优雅的解决方案。

What do you think? 你怎么看?

Angular version is 1.58 角版本为1.58

My take on this: 我对此:

If you have issue with the watching performance: You should combine angular form checking for regular input, and add a watcher on the other attributes. 如果您对观看性能有疑问:您应该结合进行常规输入的角度形式检查,并在其他属性上添加观看者。

This will save you time & performances as you will watch the variable that are not already 这将节省您的时间和性能,因为您将观看尚未存在的变量

$scope.$watch('person.image', function (newValue) {
      $scope.unsavedChanges.image = ($scope.person.image == $scope.backupCopyOfPerson.image)
}}, true);

and then upon save check if your unsavedChanges as at least 1 true 然后在保存时检查您的unsavedChanges是否至少为1 true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM