![](/img/trans.png)
[英]How can I add in Typescript typings for AngularJS $q and $http?
[英]How can I add TypeScript typings to a snippet of jQuery code for a Bootstrap multilevel dropdown?
我是 Angular 的新手,並在這篇文章之后在 Bootstrap 中制作多級下拉菜單,但那里有一些 JavaScript 代碼片段。
我無法在 TypeScript 中編寫此代碼,有人可以幫我解決這個問題嗎?
代碼如下:
$(function(){
$(".dropdown-menu > li > a.trigger").on("click",function(e){
var current=$(this).next();
var grandparent=$(this).parent().parent();
if($(this).hasClass('left-caret')||$(this).hasClass('right-caret'))
$(this).toggleClass('right-caret left-caret');
grandparent.find('.left-caret').not(this).toggleClass('right-caret left-caret');
grandparent.find(".sub-menu:visible").not(current).hide();
current.toggle();
e.stopPropagation();
});
$(".dropdown-menu > li > a:not(.trigger)").on("click",function(){
var root=$(this).closest('.dropdown');
root.find('.left-caret').toggleClass('right-caret left-caret');
root.find('.sub-menu:visible').hide();
});
});
可能您實際上並不需要轉換您的代碼,也許您只需要輸入它。
只需安裝 jquery:
npm install --save jquery
和 jquery 類型:
npm install --save-dev @types/jquery
您應該能夠通過添加類型定義來修復您的代碼。
在 angular 中使用 Jquery 可能不是一個好的選擇。 你應該使用一些 angular package ,它的組件寫在引導程序上,例如ngx-bootstrap 。
Jquery( Imperative ) 和 Angular( Declarative ) 都遵循不同的方法。 所以這不是一個好的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.