Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.JS onclick function only called on first click

I am currently working on a small aplication using Angular.JS In my view i have 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 nicely shown and the answer function is also called, everything as expected.

However, when I click the button a second time the editUser funciton is not executed. As if the onClick event from the button had been removed at dialog close.

Any help on solving this problem is greatly appreciated, Thanks

like image 802
bwright Avatar asked Oct 18 '22 20:10

bwright


1 Answers

As said here

it is probably a good idea to explicitly mention that the scope will be destroyed upon hiding the dialog (so people shouldn't pass a controller's $scope directly).

(regarding the scope you are passing to mdDialog)

So, as the scope is destroyed, angular is not binding your button with any action

like image 77
Lucas Rodriguez Avatar answered Oct 21 '22 15:10

Lucas Rodriguez