Search code examples
javascriptangularjsprompt

How would I create a custom prompt for a response in AngularJS


I basically have the UI all set up as well as all the scope variables.

Let's say I have two buttons, left and right, and I only want the function to continue when one of them is clicked. That being $scope.isLeft of $scope.isRight is true. This would be similar to the way a normal javascript prompt would wait until there is a response.

This is my infinite loop way of trying to get this accomplished (though this obviously doesn't work, it just shows what I am trying to do)

self.getLeftOrRight = function () {
    $scope.promptLeftOrRight = true;
    //this method is being called from the directive, so a call to $apply() here is necessary
    if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') {
        $scope.$apply();
    }
    while(!$scope.isLeft && !$scope.isRight) {
         //waste time for now
    }
    if (isLeft)
        return "left";
    else if (isRight)
        return "right";
};

Solution

  • If you happen to be using the Angular Bootstrap module, try looking at the modal directive here. Looking at it may also give you a better understanding of what to do.

    Otherwise I would do what @tymeJV mentioned if you are wanting to do your own thing. Have some function/directive show the prompt(also block the background with a transparent div or something) then have each button call a function to set the left or right variables.