我的应用程序中有下表,用户可以在其中设置其通知首选项。


页面加载时,将调用$ ctrl.getNotificationSettings()并将列出用户各种类别的通知首选项。请参阅屏幕截图
我还写了一个$ ctrl.save(),它允许用户更新自己的首选项并保存,效果很好。
现在我有一个带有$ ctrl.cancelSettings()的重置按钮。在这里,我希望用户能够更改一些首选项,并且如果他决定在保存表之前将其还原,则应该使用该表的首选项设置加载时的方式。在这方面需要一些帮助。


由于其他一些挑战,我无法在此处使用表格。

的HTML

  <tbody>
  <tr data-ng-repeat="app in $ctrl.notificationSettings" class="content-box">
        <td data-ng-bind="app.appName"></td>
        <td><ng-checkbox data-checked="app.email" rounded="true"></ng-checkbox></td>
        <td><ng-checkbox data-checked="app.sms" rounded="true"></ng-checkbox></td>
  </tr>
  </tbody>
  </table>
  <div class="content-box">
        <button class="ng-button-primary" data-ng-click="$ctrl.saveSettings()">Save</button>
        <button class="ng-button-secondary" data-ng-click="$ctrl.cancelSettings()">Reset</button>
  </div>


JS

            $ctrl.getNotificationSettings = function () {
                var url = "http://localhost:3000/json/notification-settings.json";
                rsicontext.getData(url).then(function (response) {
                    $ctrl.notificationSettings = response.data;
                    $ctrl.appName = response.data.appName;
                    $ctrl.emailNotification = response.data.email;
                    $ctrl.smsNotification = response.data.sms;
                });
            };


            $ctrl.cancelSettings = function () {
                console.log("cancel settings");
            };


JSON格式列出数据

    [{
    "appName":"Finance",
    "email":true,
    "sms":false
    },
    {
    "appName":"Sports",
    "email":true,
    "sms":true
    },
    {
    "appName":"Economics",
    "email":false,
    "sms":false
    },
    {
    "appName":"Health",
    "email":false,
    "sms":true
    }]


javascript - 使用angular js重置表首选项-LMLPHP

最佳答案

使用angular.copy制作原始对象的深层副本怎么办?

  $ctrl.getNotificationSettings = function () {
            var url = "http://localhost:3000/json/notification-settings.json";
            rsicontext.getData(url).then(function (response) {
                $ctrl.notificationSettings = response.data;
                $ctrl.appName = response.data.appName;
                $ctrl.emailNotification = response.data.email;
                $ctrl.smsNotification = response.data.sms;
                $scope.origData = angular.copy(response.data);
            });
        };

        $ctrl.cancelSettings = function () {
            $ctrl.notificationSettings = $scope.origData;
                $ctrl.appName = $scope.origData.appName;
                $ctrl.emailNotification = $scope.origData.email;
                $ctrl.smsNotification = $scope.origData.sms;
        };

09-26 19:15
查看更多