Javascript 在Angular2应用程序中单击图像时无法打开引导模式
我想在单击图像或按钮时打开引导模式。我已经添加了代码,但当我单击图像时,出现以下错误: 错误引用错误:$未定义(…) 以及: 错误上下文 我附上以下截图: 下面是我的Javascript 在Angular2应用程序中单击图像时无法打开引导模式,javascript,angular,typescript,twitter-bootstrap-3,Javascript,Angular,Typescript,Twitter Bootstrap 3,我想在单击图像或按钮时打开引导模式。我已经添加了代码,但当我单击图像时,出现以下错误: 错误引用错误:$未定义(…) 以及: 错误上下文 我附上以下截图: 下面是我的页面.html代码: <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bo
页面.html
代码:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<h2>Activate Modal with JavaScript</h2>
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" id="myBtn" (click)="openmodal()">Open Modal</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Modal Header</h4>
</div>
<div class="modal-body">
<p>Some text in the modal.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<img (click)="openmodal()" src="https://img.megaobzor.com/old/hardmind/artpreview/CarZaz1.jpg" class="img-thumbnail" alt="Cinque Terre" width="304" height="236">
<router-outlet></router-outlet>
我试着跟着这把小提琴-你把jquery也包括进去了吗?另外,最好使用并避免使用jQuery。是否包含jQuery?如果是,那么尝试在没有JS的情况下打开模式,但只打开HTML(查看引导示例,您可以通过向标记添加特定属性来实现)。是的,我已经在我的HTML页面上添加了它,我也在上面的问题中进行了更新,让我知道我哪里做错了确保引导脚本标记在jquery脚本标记之后我使用angular cli并且我在angular-cli.json中添加了css,我还需要调用引导js文件吗?
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
declare var $: any; //intentional use of jQuery
@Component({
selector: 'app-changeimage',
templateUrl: './changeimage.component.html',
styleUrls: ['./changeimage.component.css']
})
export class ChangeimageComponent implements OnInit {
constructor() { }
ngOnInit() {
}
openmodal(){
console.log("image clicked");
$("#myModal").modal();
$('.modal')
.prop('class', 'modal fade') // revert to default
.addClass( $(this).data('direction') );
$('.modal').modal('show');
}
}