javascript - "modal"不被 Angular 识别为元素

标签 javascript html angular typescript modal-dialog

我使用 Angular 从我的项目创建了一个组件;一切正常,直到我添加一个窗口,现在当我为我的应用程序提供服务时什么也没有出现,Angular 说“模态不是已知元素”。

HTML 文档

<modal>
<div class="modal">
  <div class="modal-body">
      <h1>A Custom Modal!</h1>
      <p>
          Home page text: <input type="text" />
      </p>
      <button (click)="closeModal('custom-modal-1');">Close</button>
   </div>
</div>
<div class="modal-background"></div>
</modal>

模态组件文件:

 import { Component, OnInit, ElementRef, Input, OnDestroy } from 
    '@angular/core';
        import {ModalWindowService} from './modal-window.service';

       @Component({
        selector: 'app-modal-window',
      templateUrl: './modal-window.component.html',
       styleUrls: ['./modal-window.component.css']
       })

       export class ModalWindowComponent implements OnInit {
     @Input() id: string;
     private element: any;

         constructor(private modalService: ModalWindowService, private el: 
  ElementRef) {
  this.element = el.nativeElement;
   }

  ngOnInit(): void {
  let modal = this;

  // ensure id attribute exists
  if (!this.id) {
      console.error('modal must have an id');
      return;
       }

    // move element to bottom of page (just before </body>) so it can be 
     displayed above everything else
     document.body.appendChild(this.element);

   // close modal on background click
    this.element.addEventListener('click', function (e: any) {
      if (e.target.className === 'modal') {
          modal.close();
      }
     });

       // add self (this modal instance) to the modal service so it's 
    accessible from controllers
  this.modalService.add(this);
   }

        // remove self from modal service when directive is destroyed
          ngOnDestroy(): void {
     this.modalService.remove(this.id);
      this.element.remove();
     }

       // open modal
        open(): void {
     this.element.style.display = 'block';
       document.body.classList.add('modal-open');
           }

        // close modal
       close(): void {
         this.element.style.display = 'none';
        document.body.classList.remove('modal-open');
       }
     }

我使用 HTML 文件作为"template",而不是在组件内部使用它。我已在我的模块中声明了该组件。

最佳答案

您的选择器名称是 app-modal-window

<app-modal-window>
<div class="modal">
  <div class="modal-body">
      <h1>A Custom Modal!</h1>
      <p>
          Home page text: <input type="text" />
      </p>
      <button (click)="closeModal('custom-modal-1');">Close</button>
   </div>
</div>
<div class="modal-background"></div>
</app-modal-window>

如果你想像<modal>那样使用它像这样更改选择器

   @Component({
    selector: 'modal',
  templateUrl: './modal-window.component.html',
   styleUrls: ['./modal-window.component.css']
   })

关于javascript - "modal"不被 Angular 识别为元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51888150/

相关文章:

javascript - 从对象的值获取父对象的名称

javascript - 使用 JS 设置 SVG 元素的属性不起作用

javascript - 获取最近选择框的值

javascript - 当我更改输入时,如何在 HTML 表单上自动显示 JavaScript 函数的结果?

javascript - 停止 JavaScript 脚本执行

javascript - 如何扩展 Angular Service Worker?

angular - 无法读取空 Angular 4 的属性 'dispose'

html - TimeLine——点击时改变颜色

javascript - 每次读取数据 block 后等待 - Node JS

javascript - jstree 类型应用不正确