【问题标题】:Bootstrap 3 Datepicker in Angular 2+ with or without jqueryAngular 2+中的Bootstrap 3 Datepicker,带或不带jquery
【发布时间】:2017-09-06 06:58:07
【问题描述】:

我是 Angular 2+ 的新手。我正在尝试将 Bootstrap 3 Datepicker 集成到 角JS2。我使用了[解决方案]:How to use Bootstrap 3 Datepicker in angular 2。但是出现了类似的错误

Cannot find name '$'

有没有办法使用或不使用 jquery 来实现任务?

下面给出我的代码。

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'});
}

我想实现 [link] 中给出的 datetimepicker:https://eonasdan.github.io/bootstrap-datetimepicker/

【问题讨论】:

    标签: jquery angular twitter-bootstrap-3 bootstrap-datetimepicker


    【解决方案1】:

    您需要添加:declare var $:any;

    declare var $: any;
    
    @Component({
        ...
    })
    

    确保您的库中有所有必需的库:

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

    然后在 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",
          ...
    

    而且你不需要在 index.html 的脚本标签中添加这些

    【讨论】:

    • 谢谢。现在错误消息消失了。但在控制台我得到$ is not a function
    • 在你的脚本标签和样式标签中添加 bootstrap-datepicker js & css:cdnjs.com/libraries/bootstrap-datetimepicker,或者通过 npm install & 在 angular-cli.json 中声明
    • 当前链接和脚本标签如下。 ` cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.47/…">
    • 给出与Uncaught Error: Bootstrap's JavaScript requires jQuery相同的错误。
    • 你能重新检查这个答案吗?我认为您需要从 npm 添加 jquery & bootstrap & moment & bootstrap-datetimepicker ,然后在 angular-cli.json 添加论文。您可以在脚本标签中删除这些
    猜你喜欢
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2015-09-22
    • 2014-10-22
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 2014-07-24
    相关资源
    最近更新 更多