Angular兩種模態框彈出方式

来源:http://www.cnblogs.com/nangxi/archive/2017/10/18/7688109.html
-Advertisement-
Play Games

本博客參考自https://github.com/cipchk/ngx-bootstrap-modal和https://valor-software.com/ngx-bootstrap/#/modals,這兩個網址幾乎涵蓋了所有的angular彈框 ...


在開始我們的blog之前,我們要先安裝ngx-bootstrap-modal

npm install ngx-bootstrap-modal --save

然後在index.html導入bootstrap樣式表

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet">

不然我們的模態框效果會難看到你想吐

一、彈出方式一(此方法來自https://github.com/cipchk/ngx-bootstrap-modal)

1.alert彈框

(1)demo目錄

--------app.component.ts

--------app.component.html

--------app.module.ts

--------detail(文件夾)

------------detail.component.ts

------------detail.component.html

(2)demo代碼

app.module.ts導入必要的BootstrapModalModule 和ModalModule ,再註冊它們

//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

//這種模態框只需要導入下麵這兩個
import { BootstrapModalModule } from 'ngx-bootstrap-modal';
import { ModalModule } from 'ngx-bootstrap/modal';

import { AppComponent } from './app.component';
import { DetailComponent } from './detail/detail.component';

@NgModule({
  declarations: [
    AppComponent,
    DetailComponent
  ],
  imports: [
    BrowserModule,
    BootstrapModalModule
  ],
  providers: [],
  entryComponents: [
    DetailComponent
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
app.module.ts

app.component.html創建一個可以彈出模態框的按鈕

<div class="container">
  <div class="row">
    <button type="button" class="btn btn-primary" (click)="showAlert()">alert模態框</button>
  </div> 
</div>
app.component.html

app.component.ts編寫這個按鈕的動作showAlert()

import { Component } from '@angular/core';
import { DialogService } from "ngx-bootstrap-modal";
import { DetailComponent } from './detail/detail.component'


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'app';
   constructor(public dialogService: DialogService) {
    }   
   showAlert() {
        this.dialogService.addDialog(DetailComponent, { title: 'Alert title!', message: 'Alert message!!!' });
    }
}
app.component.ts

detail.component.html編寫alert彈框的佈局

<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" (click)="close()" >×</button>
            <h4 class="modal-title">{{title}}</h4>
        </div>
        <div class="modal-body">
            這是alert彈框
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" (click)="close()">取消</button>
            <button type="button" class="btn btn-default">確定</button>
        </div>
    </div>
</div>
detail.component.html

detail.component.ts創建模態框組件,我們需要創建一個組件,然後由 ngx-bootstrap-model 幫忙引導啟動
對於這個組件需要繼承 DialogComponent<T, T1> 類,包含兩個參數:
T 外部傳參給模態框的類型。
T1 模態框返回值類型。
因此,DialogService應該是DialogComponent的一個構造函數的參數。

import { Component } from '@angular/core';
import { DialogComponent, DialogService } from 'ngx-bootstrap-modal';

export interface AlertModel {
  title: string;
  message: string;
}

@Component({
  selector: 'alert',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.css']
})
export class DetailComponent extends DialogComponent<AlertModel, null> implements AlertModel {
  title: string;
  message: string;
  constructor(dialogService: DialogService) {
    super(dialogService);
  }
}
detail.component.ts

2.confirm彈框

(1)demo目錄

--------app.component.ts
--------app.component.html
--------app.module.ts
--------confirm(文件夾)
------------confirm.component.ts
------------confirm.component.html

(2)demo代碼

app.module.ts導入必要的BootstrapModalModule 和ModalModule ,再註冊它們,這些都跟alert彈框一樣,因為這些都是方法一的彈出方式

//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

//這種模態框只需要導入下麵這兩個
import { BootstrapModalModule } from 'ngx-bootstrap-modal';
import { ModalModule } from 'ngx-bootstrap/modal';

import { AppComponent } from './app.component';
import { DetailComponent } from './detail/detail.component';

@NgModule({
  declarations: [
    AppComponent,
    DetailComponent
  ],
  imports: [
    BrowserModule,
    BootstrapModalModule
  ],
  providers: [],
  entryComponents: [
    DetailComponent
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
app.module.ts

app.component.html創建一個可以彈出模態框的按鈕

<div class="container">
  <div class="row">
    <button type="button" class="btn btn-primary" (click)="showConfirm()">modal模態框</button>
  </div> 
</div>
app.component.html

app.component.ts編寫這個按鈕的動作showConfirm()

import { Component } from '@angular/core';
import { DialogService } from "ngx-bootstrap-modal";
import { ConfirmComponent } from './confirm/confirm.component'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'app';
   constructor(public dialogService: DialogService,private modalService: BsModalService) {
    }
   showConfirm() {
        this.dialogService.addDialog(ConfirmComponent, {
            title: 'Confirmation',
            message: 'bla bla'
        })
            .subscribe((isConfirmed) => {
                
            });
    }
}
app.component.ts

confirm.component.html編寫confirm彈框的佈局

<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" (click)="close()" >×</button>
            <h4 class="modal-title">{{title}}</h4>
        </div>
        <div class="modal-body">
            這是confirm彈框
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-primary" (click)="close()">取消</button>
            <button type="button" class="btn btn-default">確定</button>
        </div>
    </div>
</div>
confirm.component.html

confirm.component.ts創建模態框組件

import { Component } from '@angular/core';
import { DialogComponent, DialogService } from 'ngx-bootstrap-modal';

export interface ConfirmModel {
  title:string;
  message:any;
}

@Component({
  selector: 'confirm',
  templateUrl: './confirm.component.html',
  styleUrls: ['./confirm.component.css']
})
export class ConfirmComponent extends DialogComponent<ConfirmModel, boolean> implements ConfirmModel {
  title: string;
  message: any;
  constructor(dialogService: DialogService) {
    super(dialogService);
  }
  confirm() {
    // on click on confirm button we set dialog result as true,
    // ten we can get dialog result from caller code
    this.close(true);
  }
  cancel() {
    this.close(false);
  }
}
confirm.component.ts

3.內置彈框

(1)demo目錄

--------app.component.ts
--------app.component.html
--------app.module.ts

(2)demo代碼

內置彈框也包括 alert confirm prompt 三種形態,都有一些內置的樣式

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { BootstrapModalModule } from 'ngx-bootstrap-modal';
import { ModalModule } from 'ngx-bootstrap/modal';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BootstrapModalModule,
    ModalModule.forRoot()
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
app.module.ts

app.component.html很簡單,就一個按鈕

<div class="container">
  <div class="row">
    <button type="button" class="btn btn-default" (click)="show()">內置</button>
  </div> 
</div>
app.component.html

app.component.ts很簡單,連組件的佈局都不用寫,傳入一些參數比如圖標icon,大小size等

import { Component } from '@angular/core';
import { DialogService, BuiltInOptions } from "ngx-bootstrap-modal";

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'app';

   constructor(public dialogService: DialogService) {
    }
    show(){
      this.dialogService.show(<BuiltInOptions>{
          content: '保存成功',
          icon: 'success',
          size: 'sm',
          showCancelButton: false
      })
    }
}
app.component.ts

二、彈出方式二(此方法來自https://valor-software.com/ngx-bootstrap/#/modals)

還是跟上一種方法一樣,先安裝ngx-bootstrap-modal,然後導入bootstrap樣式表

1.demo目錄

--------app.component.ts
--------app.component.html
--------app.module.ts

2.demo代碼

app.module.ts導入相應模塊,並且註冊它們

//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { ModalModule } from 'ngx-bootstrap/modal';
import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    ModalModule.forRoot()
  ],
  providers: [],
  entryComponents: [
    
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
app.module.ts

app.component.ts

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

import { BsModalService } from 'ngx-bootstrap/modal';
import { BsModalRef } from 'ngx-bootstrap/modal/modal-options.class';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
   title = 'app';
   public modalRef: BsModalRef;
   constructor(private modalService: BsModalService) {
    }
    showSecond(template: TemplateRef<any>){//傳入的是一個組件
        this.modalRef = this.modalService.show(template,{class: 'modal-lg'});//在這裡通過BsModalService裡面的show方法把它顯示出來

   };
}
app.component.ts

app.component.html

<div class="container">
  <div class="row">
    <button type="button" class="btn btn-success" (click)="showSecond(Template)">第二種彈出方式</button>
  </div> 
</div>

<!--第二種彈出方法的組件-->
<template #Template>
  <div class="modal-header tips-modal-header">
    <h4 class="modal-title pull-left">第二種模態框</h4>
    <button type="button" class="close pull-right" aria-label="Close" (click)="modalRef.hide()">
      <span aria-hidden="true">×</span>
    </button>
  </div>
  <div class="modal-body tips-modal-body">
    <div class="tips-contain"><span>第二種模態框彈出方式</span></div>
  </div>
  <div class="modal-footer">
      <button type="button" class="btn btn-default" (click)="modalRef.hide()">確定</button>
      <button type="button" class="btn btn-default" (click)="modalRef.hide()">取消</button>
  </div>
</template>
app.component.html

三、最終效果

我們將上面所有的彈框全部寫在一起,然後效果就是這樣的

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 繼承:當前對象沒有的屬性和方法,別人有,拿來給自己用,就是繼承 1 混入式繼承 2.原型繼承 a) 給原型對象添加新成員(通過對象的動態特性),不是嚴格意義上的繼承 ,,,,實例對象繼承了原型 b) 直接替換原型對象 c) 利用混入的方式給原型對象添加成員 3.經典繼承 js var 對象1 = O ...
  • 運算符用於執行程式代碼運算,會針對一個及以上操作數項目來進行運算。2+3,其操作數是2和3,而運算符則是“+”。上一篇我們說過變數用來存儲數據,而同一個變數中的數據在不同的時刻可以不同,在程式的運行過程中,我們根據需要將數據進行相應規則的運算以得到預期的結果,運算符則是其中運算的規則。 運算符在js ...
  • 移動端用zepto做的頁面,突然發現on綁定的click事件並沒有觸發,代碼如下: 我把此寫法改成非事件委托的方式,發現該click事件就能觸發 最後$(document)換成$('body')或者$('ul')試試,實驗結果是: $(document) 不行 $(‘body') 不行 $(‘ul' ...
  • 本文是Javascript高級程式設計 第1章的筆記,主要介紹了: Javascript和ECMAScript的關係; 宿主環境; DOM和API的相關概念 ...
  • 要考慮函數可被可重覆使用(調用),需要將可變化的變為參數封裝起來 table載入成功寫的函數,是因為我自己需要才寫的。把table里的數據放在全局變數後,查詢詳細信息就不用再做ajax。這個'load-success.bs.table'api我還有個問題,當這個table被多次load-succes ...
  • 絕對地址和相對地址是網站開發基礎知識中很重要的知識點。1.在網頁中插入文件時,不可以使用硬碟的絕對路徑 舉個例子,在網頁中插入圖片,代碼如下: <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/ht ...
  • 前言 編程時我們往往拿到的是業務流程正確的業務說明文檔或規範,但實際開發中卻佈滿荊棘和例外情況,而這些例外中包含業務用例的例外,也包含技術上的例外。對於業務用例的例外我們別無它法,必須要求實施人員與用戶共同提供合理的解決方案;而技術上的例外,則必須由我們碼農們手刃之,而這也是我想記錄的內容。 我打算 ...
  • 轉載請註明出處 http://www.023996.cn/hyxw/s1129.html Anjularjs表單驗證 能夠根據用戶在表單中輸入的內容給出實時視覺反饋是非常重要的。在人與人溝通的語境中,表單驗證給出來的反饋同獲得正確輸入同等重要。 表單驗證不僅能給用戶提供有用的反饋,同時也能保護我們的 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...