简体   繁体   English

Ng-bootstrap不呈现

[英]Ng-bootstrap not rendering

I am trying to render a modal like so: 我试图像这样呈现一个模态:

HTML HTML

<div class="btn-holder">
            <a (click)="this.open()" class="btn btn-success text-uppercase">Edit My Profile</a>
        </div>

Module: 模块:

imports: [BrowserModule, HttpModule,
    RouterModule.forRoot(appRoutes),
      NgbModule.forRoot(),
    EditProfileComponent

  ],

Component: 零件:

import {Component} from "@angular/core";

@Component({
    selector: 'edit-profile',
    templateUrl: './editProfile.html'
})
export class EditProfileComponent{

    constructor(){

    }

    submitForm(){

    }

}

The issue is I am not sure how to get it working because the doc are vague. 问题是我不知道如何让它工作,因为文档含糊不清。 Advice? 建议吗?

I have tried the following: 我尝试过以下方法:

@Component({
    selector: 'edit-profile',
    templateUrl: './editProfile.html'
})
export class EditProfileComponent{

    closeResult: string;

    constructor(private modalService: NgbModal){ }

    open(content) {
        this.modalService.open(content).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}`;
        }
    }

}

HTML: HTML:

  <a (click)="open(content)" class="btn btn-success text-uppercase">Edit My Profile</a>
</div>

Error in console when I click the button: 单击按钮时控制台出错:

ERROR TypeError: _co.open is not a function
    at Object.eval [as handleEvent] (ProfileComponent.html:46)
    at handleEvent (core.es5.js:12047)
    at callWithDebugContext (core.es5.js:13508)
    at Object.debugHandleEvent [as handleEvent] (core.es5.js:13096)
    at dispatchEvent (core.es5.js:8659)
    at core.es5.js:9270
    at HTMLAnchorElement.<anonymous> (platform-browser.es5.js:2668)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:424)
    at Object.onInvokeTask (core.es5.js:3924)
    at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)

I have look at the plunker examples but when I implement them it seems to break my app. 我看过plunker示例,但是当我实现它们时,它似乎打破了我的应用程序。 I added the component and dependency to app.module. 我将组件和依赖项添加到app.module。

Advice? 建议吗?

If you are trying to Display a Modal you can directly use Bootstrap in your Angular . 如果您尝试显示模态,可以直接在Angular中使用Bootstrap。 Like So 像那样

npm install bootstrap --save npm install bootstrap --save

In Angular-cli.json 在Angular-cli.json中

  "styles": [
    "styles.css",
    "../node_modules/bootstrap/dist/css/bootstrap.min.css"
  ],
  "scripts": ["../node_modules/jquery/dist/jquery.js",
    "../node_modules/bootstrap/dist/js/bootstrap.min.js"
  ],

IN COMPONENT 在组件中

import { Component } from '@angular/core';

declare var $ :any;

@Component({

For more info on how to import third party lib LINK 有关如何导入第三方lib LINK的更多信息

Working Modal - LINK . 工作模式 - 链接

and if you want to check the source code for the working modal LINK . 如果你想检查工作模式LINK的源代码。

Working Plunker Link: http://plnkr.co/edit/vTY9gG9QcL25Wm9NTBqS?p=preview Working Plunker Link: http ://plnkr.co/edit/vTY9gG9QcL25Wm9NTBqS?p = preview

1) In your app.module.ts, Looks like you are not declaring EditProfileComponent . 1)在你的app.module.ts中,看起来你没有声明EditProfileComponent Instead of imports, put EditProfileComponent in declarations. 而不是导入,将EditProfileComponent放在声明中。 Refer to the code below: 请参阅以下代码:

@NgModule({
  imports: [BrowserModule,HttpModule,
RouterModule.forRoot(appRoutes), NgbModule.forRoot()], 
  declarations: [App, EditProfileComponent]
  bootstrap: [App]
})

2) Your component looks good: 2)你的组件看起来很好:

    import {Component} from '@angular/core';
    import {NgbModal, ModalDismissReasons} from '@ng-bootstrap/ng-bootstrap';

    @Component({
      selector: 'edit-profile',
      templateUrl: './editProfile.html'
    })
    export class EditProfileComponent {
      closeResult: string;

      constructor(private modalService: NgbModal) {}

      open(content) {
        this.modalService.open(content).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}`;
        }
      }
    }

3) In your HTML edit-profile.html, you seem to be missing the ng-template that would be referred to display Modal. 3)在您的HTML edit-profile.html中,您似乎缺少将被引用到显示模式的ng-template。

If you notice, we pass 'content' in the function when we click on 'a' tag. 如果您注意到,我们在点击“a”标记时会在函数中传递“内容”。 This 'content' is the local reference to the template in the html. 这个'content'是html中模板的本地引用。 We use the instance 'this.modalService' of 'NgbModal' to open the specific modal in our component. 我们使用'NgbModal'的实例'this.modalService'来打开组件中的特定模态。

<ng-template #content let-c="close" let-d="dismiss">
  <div class="modal-header">
    <h4 class="modal-title">Modal title</h4>
    <button type="button" class="close" aria-label="Close" (click)="d('Cross click')">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <p>One fine body&hellip;</p>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" (click)="c('Close click')">Close</button>
  </div>
</ng-template>


<a (click)="open(content)" class="btn btn-success text-uppercase">Edit My Profile</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM