[英]How do I call a js plugin from within a directives link method - Angularjs
I am trying to wrap Bootstrap 3's modal in a Angular directive. 我正在尝试将Bootstrap 3的模式包装在Angular指令中。 I get into the link function, but I am not able to call the Bootstrap
.modal()
function. 我进入了链接功能,但是无法调用Bootstrap
.modal()
函数。 Here is my html: 这是我的html:
<h1>This is a test!!!</h1>
<button ng-click="test()">This is a button</button>
<div id="myModal" class="modal fade" bsmodal>
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body…</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
And here is my directive: 这是我的指令:
App.directive('bsmodal', function() {
return {
restrict: 'A',
link: function(scope, element, attrs) {
element.modal('show');
}
};
});
When I get to the element.modal('show')
, I get the error, TypeError: Object #<Object> has no method 'modal'
. 当我到达
element.modal('show')
,出现错误TypeError: Object #<Object> has no method 'modal'
。 The element
object looks like a jQuery
wrapped object, but it still does not have the method. element
对象看起来像是jQuery
包装的对象,但是它仍然没有方法。 The part I don't understand is I can call $('myModal').modal()
in the link function and everything works like it is supposed to work. 我不了解的部分是我可以在链接函数中调用
$('myModal').modal()
,并且一切正常,应该可以正常工作。
So my questions are 所以我的问题是
element
and a jQuery
wrapped object? element
和jQuery
包装的对象有什么区别? (They look the same) modal()
function on the element
inside the link function? element
上调用modal()
函数? Angular uses a reduced version of jQuery
, jqLite
which supports a subset of the functions offered by jQuery . Angular使用的是
jQuery
的简化版本jqLite
,它支持jQuery提供的功能的子集 。
It looks like you are loading jQuery anyway in your page. 看来您仍在页面中加载jQuery。 If you load it before angular, angular will use it instead of using
jqLite
and you should be able to call element.modal('show')
. 如果您在 angular 之前加载它,angular将使用它而不是
jqLite
,您应该能够调用element.modal('show')
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.