私の(表)概要には、カスタムディレクティブがあります。このディレクティブには、顧客の詳細が表示され、編集ボタンが含まれています。このボタンをクリックすると、編集フォームがポップアップします。顧客の価値観を変更すると、詳細も更新されることがわかります。これはもちろんバインディングのためであり、まさに私が欲しいものです。
しかし、編集フォームでキャンセルを押すと、元の値を復元したいのですが、これがうまくいきません。問題は、詳細に編集された値が含まれており、編集された値が復元されないことです。
コードの重要な部分を含む plunker を作成しました。機能しませんが、すべてをどのように設定したかがわかります。
var module = angular.module('app', []);
module.directive('customerDetails', function ($http, CustomerService) {
return {
restrict: 'E',
templateUrl: '/Customer/View',
scope: {
customerId: '=',
showEditCustomerForm: '&customerEditForm'
},
link: function (scope, element, attr) {
scope.customerData = {};
CustomerService.getCustomerById(scope.customerId).then(function (response) {
scope.customerData.customer = response;
});
}
}
});
module.controller('DemoCtrl', function ($scope) {
$scope.showEditCustomerForm = function (customer) {
$scope.customerFormData = {};
$scope.customerFormData.customer = customer;
$scope.customerFormData.originalCustomer = angular.copy(customer);
$scope.showAddCustomer = true;
$scope.showOverlay = true;
};
$scope.hideAddCustomerForm = function (restoreOriginal) {
if (restoreOriginal) {
// Here I want to restore the original customer to discard the changes, but this doesn't work
// The View is not updated
$scope.customerFormData.customer = $scope.customerFormData.originalCustomer;
}
$scope.showAddCustomer = false;
$scope.showOverlay = false;
}
});
<body ng-controller="DemoCtrl">
<table>
<tbody id="customerRows" data-ng-repeat="customer in customers">
<tr>
<td>
<customer-Details customer-Id="customer.Id" customer-edit-form="showEditCustomerForm(customer)"></customer-Details>
</td>
</tr>
</tbody>
</table>
<div data-ng-switch="showAddCustomer">
<div data-ng-switch-when="true">
<div class="overlay">
</div>
<div ng-include="'/Customer/Add'"></div>
</div>
</div>
</body>