0

I want to include HMTL for Bootstrap modal on click but I cannot get it to work.

In my controller I have this code:

  $scope.onFollowingClick = function () {
        console.log('clicked');
        $scope.isFollowingModal = true;
        $('#FollowingModal').modal('toggle');
    };

And my HTML looks like this:

<div ng-if="isFollowingModal">
    <div class="modal modal-alternate fade" ng-include="'/Templates/Modals/Public/ModalFollowing.html'" id="FollowingModal"> </div>
</div>

And when I click button nothing happens ("clicked" is logged in console), in browser when I inspect element, I only see:

<!-- ngIf: isFollowingModal -->

And no network request to fetch ModalFollowing.html is logged.

What am I doing wrong ?

6
  • You're trying to use a jQuery plugin inside an AngularJS controller -- it's possible, just not recommended. Try an AngularJS alternative: angular-ui.github.io/bootstrap/#/modal or mgcrea.github.io/angular-strap/#/modals. Commented Jun 30, 2015 at 19:23
  • even if request is made, you aren't accounting for asynchronous loading before trying to activate the modal. That's what directives are used for Commented Jun 30, 2015 at 19:28
  • @charlietfl what you are saying is completely true but this doesn't explain why request isn't made :/ Commented Jun 30, 2015 at 19:33
  • Use $watch in scope it will help to solve your problem Commented Jun 30, 2015 at 20:04
  • 1
    @hyperN Sorry without watch only its working check this plunker code am i getting correct your you have different problem plnkr.co/edit/n7idBSY5RKoI7UT5IzRH?p=preview Commented Jun 30, 2015 at 20:18

1 Answer 1

1

If we go this way, using $http service is really helpful even though ngInclude fetches, compiles and includes an external HTML fragment.

$scope.onButtonClick = function() {
  $http
    .get('/Templates/Modals/Public/ModalFollowing.html')
    .success(function(response) {
      $('modal')
        .html(response)
        .modal('show');
    });
}

And never forget about async :P

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

3 Comments

Thanks for the answer I'll check it when I come home :)
I tried your solution, and it partially worked, HTML was injected, and modal opened but for some reason controller doesn't work (and it is called inside html)
It won't work, because you have to compile that again using $compile. Check the angular docs for that :P

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.