Itsik Mauyhas
Itsik Mauyhas

Reputation: 3984

Angular and jQuery modal - undefined is not a function

I am creating a modal using Angular and jQuery, and I new to jQuery so I am using this directive angular+jQuery modal.

And I am getting this error:

undefined is not a function

For :

$(element).modal('show');

When changing it to

$element.modal('show');

The error changes to

$element is not defined.

My code for the directive:

mymodal.directive('modal', function () {
    return {
      template: '<div class="modal fade">' + 
          '<div class="modal-dialog">' + 
            '<div class="modal-content">' + 
              '<div class="modal-header">' + 
                '<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>' + 
                '<h4 class="modal-title">{{ title }}</h4>' + 
              '</div>' + 
              '<div class="modal-body" ng-transclude></div>' + 
            '</div>' + 
          '</div>' + 
        '</div>',
      restrict: 'E',
      transclude: true,
      replace:true,
      scope:true,
      link: function postLink(scope, element, attrs) {
        scope.title = attrs.title;

        scope.$watch(attrs.visible, function(value){
          if(value == true)
            $(element).modal('show');
          else
            $(element).modal('hide');
        });

        $(element).on('shown.bs.modal', function(){
          scope.$apply(function(){
            scope.$parent[attrs.visible] = true;
          });
        });

        $(element).on('hidden.bs.modal', function(){
          scope.$apply(function(){
            scope.$parent[attrs.visible] = false;
          });
        });
      }
    };
  });

Thanks for any help! if there is more code needed I will post it.

Upvotes: 0

Views: 734

Answers (1)

Yin Gang
Yin Gang

Reputation: 1443

Fixed Demo, guess your code is totally correct.

Why not working, as the bootstrap.min.js loaded failed.

I just use https for bootstrap.min.js and update jQuery to 2.0.0, then it worked.

Upvotes: 1

Related Questions