Angular Onclick function for first click only

I am currently working on a small application using Angular.JS. In my opinion, I have the following button

<md-button class="md-primary" ng-click="editUser(user, $event)">Edit</md-button> 

The editUser method looks something like this:

 $scope.editUser = function (user, $event) { $scope.userToEdit = user; $mdDialog.show({ controller: DialogController, targetEvent: $event, templateUrl: '/js/modules/user/views/edit.tmpl.html', parent: angular.element(document.body), clickOutsideToClose: true, scope: $scope }) . then(function (answer) { if (answer == "save") { for (right in $scope.allSystemRightsStatements) { if ($scope.allSystemRightsStatements[right].selected) { if( $scope.userToEdit.rights==null){ $scope.userToEdit.rights = []; } $scope.userToEdit.rights.push($scope.allSystemRightsStatements[right]); } } $scope.updateUser($scope.userToEdit); } $scope.userToEdit = {}; }, function () { $scope.userToEdit = {}; }); }; $scope.updateUser = function (user) { //userService.updateUser makes a $http PUT request var promise = userService.updateUser(user); promise.then(function (result) { $mdToast.show( $mdToast.simple(result.message) .position($scope.getToastPosition()) .hideDelay(3000) ); }, function (reason) { $mdToast.show( $mdToast.simple(reason) .position($scope.getToastPosition()) .hideDelay(3000) ); }, function (update) { }); }; 

Now the dialog is well displayed, and the response function is also called as expected.

However, when I click the button, the second time the editUser function is not executed. As if the onClick event from the button was deleted when the dialog box was closed.

Any help to solve this problem is much appreciated. Thanks

+5
source share
1 answer

As said here

it would probably be nice to explicitly indicate that the area will be destroyed when the dialog is hidden (so people should not pass the $ scope checksum directly).

(regarding the scope you pass to mdDialog)

So, as the area is destroyed, angular does not associate your button with any action

+3
source

Source: https://habr.com/ru/post/1238351/


All Articles