[英]Jquery not working in angular2
I am new in Angular2. 我是Angular2的新手。 I have done this once, but when I am trying to set up the same environment in my home PC, I am unable to do this. 我曾经做过一次,但是当我尝试在家用PC中设置相同的环境时,我将无法执行此操作。
In my add-page.component.ts , I am declaring $
and jquery
like this:- 在我的add-page.component.ts中 ,我这样声明$
和jquery
:
declare var $ : any;
declare var jquery : any;
Then in the ngAfterViewInit
function, I am running this jquery. 然后在ngAfterViewInit
函数中,我正在运行此jquery。
ngAfterViewInit(){
$(function(){
if($('#desc').length>0)
{
$("#desc").Editor();
$(".Editor-editor").html($('#desc').text());
$("#desc").val($('.Editor-editor').html());
}
});
}
However, the jquery isn't working. 但是,jQuery无法正常工作。 When I am writing alert('Hi')
inside ngAfterViewInit()
but outside $(document).ready(function(){})
, I am getting the alert. 当我在ngAfterViewInit()
但在$(document).ready(function(){})
之外编写alert('Hi')
$(document).ready(function(){})
,我得到了警报。 But alert inside $(document).ready(function(){})
isn't working. 但是$(document).ready(function(){})
内部的警报不起作用。 So, that means the ngAfterViewInit
is working but not the $(document).ready(function(){})
. 因此,这意味着ngAfterViewInit
可以正常工作,但$(document).ready(function(){})
却无法工作。
By the way, jquery library is added in the index.html file:- 顺便说一下,jQuery库已添加到index.html文件中:
<script src="assets/admin-assets/bower_components/jquery/jquery.min.js"></script>
What am I doing wrong? 我究竟做错了什么?
You don't need this line of code: 您不需要以下代码行:
declare var jquery : any;
Also make sure that you importing ngAfterViewInit from angular using this line of code: 还要确保使用以下代码行从angular导入ngAfterViewInit:
import { ngAfterViewInit } from '@angular/core';
also make sure that you jquery file exists in the components folder, if you don't know how, try to use the CDN by changing the reference to be "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"
instead in your script tag. 还请确保您的jquery文件存在于components文件夹中,如果您不知道如何操作,请通过将引用更改为"https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"
来尝试使用CDN "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"
而不是在脚本标签中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.