javascript – 只在第一次点击时调用Angular.JS onclick函数

前端之家收集整理的这篇文章主要介绍了javascript – 只在第一次点击时调用Angular.JS onclick函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我目前正在使用Angular.JS开发一个小应用程序
在我看来,我有以下按钮

editUser方法看起来像这样:

$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) {
    });
};

现在,对话框很好地显示,并且还调用了答案功能,一切都按预期进行.

但是,当我第二次单击该按钮时,不会执行editUser功能.好像在关闭对话框中删除了按钮中的onClick事件.

非常感谢任何帮助解决这个问题,
谢谢

最佳答案
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).

(关于你传递给mdDialog的范围)

因此,当范围被破坏时,angular不会将您的按钮与任何操作绑定

原文链接:https://www.f2er.com/js/428930.html

猜你在找的JavaScript相关文章