【问题标题】:Toggle and Modals are not working on Angular appToggle 和 Modals 不适用于 Angular 应用
【发布时间】:2019-10-15 17:15:06
【问题描述】:

我是 Angular 新手,在让 Bootstrap 切换和 Modal 工作时遇到了一些问题。 BootStrap CSS 文件正在工作,因为表单正在接受样式;但是,我认为 JS 文件没有正确链接。请让我知道我在做什么。

我使用 Angular CLI 对 BootStrap 和 jQuery 进行了 NPM 安装。 安装后,我将 BootStrap 和 jQuery 文件链接到 angular.json 文件,以便应用程序能够使用这些库。

"build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/LyndaChapter1",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            **"styles": [
              **"node_modules/bootstrap/dist/css/bootstrap.css",
              "src/styles.css"**
            ],
            "scripts": [
              "node_modules/bootstrap/dist/js/bootstrap.js",
              "node_modules/jquery/dist/jquery.js"
            ],**
            "es5BrowserSupport": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                }
              ]
            }
          }
        },

预期结果:切换和模态应该可以工作 实际结果:两者都不起作用

【问题讨论】:

  • 你试过把jquery dep放在第一位吗? bootstrap依赖jquery,所以需要先加载。
  • 与其依赖 jQuery (应该避免),已经有一个可以使用的引导角度组件包。
  • @Jacquesジャック,谢谢你,我认为你的建议成功了。
  • 我将其添加为答案。

标签: angular bootstrap-4 bootstrap-modal angular7 angular-bootstrap-toggle


【解决方案1】:

虽然我同意这里关于不使用 jQuery 的其他答案,但如果您打算将它与 bootstrap 或任何其他 依赖 的库一起使用,则必须先加载 jQuery 。

所以,要解决您的问题,只需将 jQuery 依赖项置于引导依赖项之上。

【讨论】:

    【解决方案2】:

    尝试将这些包添加到您的 package.json 文件并在 Package.json 文件级别运行 npm install --save 命令。

    /* "@ng-bootstrap/ng-bootstrap": "^4.1.0", “引导程序”:“^4.3.1”*/

    【讨论】:

      【解决方案3】:

      jQuery 是在 Angular 上运行的糟糕选择...您可能需要的是 ngBootstrap

      那里有很好的样板代码,您可以像 i did here 一样使用它们

      相关HTML:

      <ng-template #content let-modal>
        <div class="modal-header">
          <h4 class="modal-title" id="modal-basic-title">Profile update</h4>
          <button type="button" class="close" aria-label="Close" (click)="modal.dismiss('Cross click')">
            <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
          <form>
            <div class="form-group">
              <label for="dateOfBirth">Date of birth</label>
              <div class="input-group">
                <input id="dateOfBirth" class="form-control" placeholder="yyyy-mm-dd" name="dp" ngbDatepicker #dp="ngbDatepicker">
                <div class="input-group-append">
                  <button class="btn btn-outline-secondary calendar" (click)="dp.toggle()" type="button"></button>
                </div>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-outline-dark" (click)="modal.close('Save click')">Save</button>
        </div>
      </ng-template>
      
      <button class="btn btn-lg btn-outline-primary" (click)="open(content)">Launch demo modal</button>
      
      <hr>
      
      <pre>{{closeResult}}</pre>
      
      <hr/>
      
      <p>
        <button type="button" class="btn btn-outline-primary" (click)="isCollapsed = !isCollapsed"
                [attr.aria-expanded]="!isCollapsed" aria-controls="collapseExample">
          Toggle
        </button>
      </p>
      <div id="collapseExample" [ngbCollapse]="isCollapsed">
        <div class="card">
          <div class="card-body">
            You can collapse this card by clicking Toggle
          </div>
        </div>
      </div>
      

      相关TS:

      import {Component} from '@angular/core';
      import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';
      
      @Component({
        selector: 'ngbd-modal-basic',
        templateUrl: './modal-basic.html'
      })
      export class NgbdModalBasic {
        closeResult: string;
        public isCollapsed = false;
      
        constructor(private modalService: NgbModal) {}
      
        open(content) {
          this.modalService.open(content, {ariaLabelledBy: 'modal-basic-title'}).result.then((result) => {
            this.closeResult = `Closed with: ${result}`;
          }, (reason) => {
            this.closeResult = `Dismissed ${this.getDismissReason(reason)}`;
          });
        }
      
        private getDismissReason(reason: any): string {
          if (reason === ModalDismissReasons.ESC) {
            return 'by pressing ESC';
          } else if (reason === ModalDismissReasons.BACKDROP_CLICK) {
            return 'by clicking on a backdrop';
          } else {
            return  `with: ${reason}`;
          }
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多