[英]how to create an angular directive to load jquery?
我想在我的mvc + angular js項目中使用jSignature jquery插件,但是由於我沒有正確的指令來加載它,因此無法在項目中加載jquery,需要幫助。
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/jquery-ui.min.js"></script>
<script src="jquery.signature.js"></script>
<script>
$(function () {
$('#sig').signature();
$('#clear').click(function () {
$('#sig').signature('clear');
});
$('#json').click(function () {
alert($('#sig').signature('toJSON'));
});
$('#svg').click(function () {
alert($('#sig').signature('toSVG'));
});
});
</script>
</head>
<body>
這是我與jquery插件一起使用的代碼,但是需要一個指令才能正確加載jquery,編寫指令需要一些幫助嗎?
您不需要指令來加載 jQuery。 只是在包含AngularJS之前包含jQuery,angular將使用jQuery而不是jQlite。
包含第三方js代碼的最佳實踐是創建一個將代碼包裝在link
函數中的指令:
app.directive('myDir', function() {
return {
link:link
};
function link($scope, $element, $attr){
(function ($) {
$('#sig').signature();
$('#clear').click(function () {
$('#sig').signature('clear');
});
$('#json').click(function () {
alert($('#sig').signature('toJSON'));
});
$('#svg').click(function () {
alert($('#sig').signature('toSVG'));
});
}(jQuery));
});
});
最后在需要的地方包含該指令。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.