简体   繁体   中英

Disable the two way binding in angular?

I'm using the Angular material md-dialog , when the user click the button (edit button) to show the dialog, it takes the current object (a row in a table) and pass it to the showDialog function, fill the dialog fields with its values:

<button ng-class="showDialog(task)">Open dialog</button>

showDialog function:

$scope.showDialog = function (task) {
    $scope.taskToEdit = task;
    $mdDialog.show({
        controller: DialogController,
        contentElement: '#taskEdit',
        parent: angular.element(document.body),
        clickOutsideToClose: true
    });
};

But whenever I edit the values in the dialog there is a live binding in the back in the table, where the table is getting the same keystrokes values as the the dialog, and if I clicked outside (cancel the edits) these values stay persist. So is there a way to disable this two way binding and just pass a copy of the object to the showDialog function not a reference?

In this case angular.copy() should do it. There are a lot of approaches to this but you should use a simple solution - thats why angular.copy() should do it this time - try:

$scope.taskToEdit = angular.copy(task);

This does not disable the E2E binding in AngularJS but it will prevent updating the $scope in your main view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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