-->

Bootstrap 3 Datepicker in Angular 2+ with or witho

2019-05-25 21:10发布

问题:

I am new to Angular 2+. I am trying to integrate Bootstrap 3 Datepicker in AngularJS2. I used the [solution]: How to use Bootstrap 3 Datepicker in angular 2. But got error like

Cannot find name '$'

Is there any way to implement the task with or without jquery?

My code given below.

index.html

 <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
 <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
 <script
 src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"
 integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30="
 crossorigin="anonymous"></script>
 <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">   </script>

new-job.component.html

<div class="mdl-textfield mdl-js-textfield mdl-textfield--floating-label">
   <input type='text' class="form-control" id='proposed_start_at' formControlName="proposed_start_at" #proposed_start_at/>
   <label class="mdl-textfield__label" for="proposed_start_at">Start Time</label>
</div>

new-job.component.ts

@ViewChild("proposed_start_at")
public startDateElementRef: ElementRef;

ngAfterViewInit() {
    $(this.startDateElementRef.nativeElement).datetimepicker({format: 'LT'});
}

I want to implement datetimepicker which is given in [link]: https://eonasdan.github.io/bootstrap-datetimepicker/

回答1:

you need to add: declare var $:any;

declare var $: any;

@Component({
    ...
})

Be sure to have all required librairies in your librairies:

npm install jquery bootstrap moment eonasdan-bootstrap-datetimepicker--save

then in angular-cli.json

"styles": ["../node_modules/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css"],
"scripts": [
      "../node_modules/jquery/dist/jquery.js",
      "../node_modules/bootstrap/dist/js/bootstrap.js",
      "../node_modules/moment/moment.js",
      "../node_modules/eonasdan-bootstrap-datetimepicker/src/js/bootstrap-datetimepicker.js",
      ...

And you dont need to add these in scripts tags in index.html