0

I'm trying to use Bootstrap 3 modals with AngularJS. I have two buttons: Log in and Sign up, and after clicking on them I want a modal to pop up. Based on which button user has chosen, I need to display appropriate content. To manage this, I use ng-show and ng-hide inside modals. Everything works fine, but in this piece of code my variable isRegistrationPage doesn't change its state.

button.btn.btn-default.navbar-btn.btn-info ng-click="isRegistrationPage=false;open('sm')" Log in

button.btn.btn-default.navbar-btn.btn-warning ng-click="isRegistrationPage=true;open('sm')" Sign up

I'm using almost raw JS part from https://angular-ui.github.io/bootstrap/#/modal

Is it any other good way to manage my goal?

4
  • Scoping issue? Is this inside a repeater? Commented Apr 21, 2015 at 19:40
  • isRegistrationPage is not in in scope but in HTML part Commented Apr 21, 2015 at 19:47
  • I think you just solved your own problem with that comment @MarcinMantke. Don't mix plain javascript with Angular. If you're using a js-library, use it all the way. Commented Apr 21, 2015 at 20:04
  • I don't mix plain JS with Angular. What I ment was that I use isRegistrationPage within HTML code, like that: ng-init="isRegistrationPage" and change it later by ng-click="isRegistrationPage=false or so. But when I need to open modal AND change isRegistrationPage then it won't work. Commented Apr 21, 2015 at 22:36

2 Answers 2

1

You could have two functions in your controller, one per button, then call open('sm') from there.

Something like this:

$scope.onLoginClicked = function(){
    $scope.isRegistrationPage = false;
    open('sm');
}
$scope.onSignupClicked = function(){
    $scope.isRegistrationPage = true;
    open('sm');
}

Then just use them on the ng-click

Sign up to request clarification or add additional context in comments.

1 Comment

I followed your answer and it was almost correct. Thing is, that with using angular-ui bootstrap modal, you have to use $rootScope.isRegistrationPage, not just $scope.isRegistrationPage. But apart from that everything was fine and led me straight into correct answer.
0

I think you try to use the modal service in a wrong way, especially how you want to pass through the isRegistrationPage variable.

Take a look at the code snippets below:

Here you can see part of my controller where the editBlogEntry method is responsible for opening the modal window. Note the resolve part where I can pass through variables.

function editBlogEntry(blogEntryId)
        {
            var modalInstance = $modal.open({
                templateUrl: '/ui/app/dilib/partials/blog/blog-entries/blog-entry-form.html',
                controller: 'editBlogEntryController as vm',
                size: 'lg',
                resolve: {
                    selectedBlogEntryId: function ()
                    {
                        return blogEntryId;
                    }
                }
            });

            modalInstance.result
                .then(function (result)
                {
                    logSuccess('Blog entry is modified!');
                    $route.reload();
                });

        }

This is part of the controller which will be instantiated when the modal window is opened. Note the selectedBlogEntryId which is the passed through variable.

var controllerId = 'editBlogEntryController';

    angular
        .module('dilibApp')
        .controller(controllerId, ['$scope', '$modalInstance', 'selectedBlogEntryId', 'common', 'datacontext', 'dataservice.blogentry.helper', editBlogEntryController]);

    function editBlogEntryController($scope, $modalInstance, selectedBlogEntryId, common, datacontext, dataServiceBlogEntryHelper)
    { /*... javascript magic ...*/}

HTML part, where you can see that the edit method is bound to a button element.

<button class="btn" ng-model="blogEntry.id" ng-click="vm.editBlogEntry(blogEntry.id)">edit</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.