本文主要介紹了Angular彈出模態框的兩種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下,希望能幫助到大家。
在開始我們的blog之前,我們要先安裝ngx-bootstrap-modal
npm install ngx-bootstrap-modal --save
不然我們的模態框效果會難看到你想吐
一、彈出方式一(此方法來自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.component.html建立一個可以彈出模態框的按鈕
<p> </p><p> <button>alert模态框</button> </p>
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!!!' }); } }
detail.component .html編寫alert彈框的佈局
<p> </p><p> </p><p> <button>×</button> </p><h4 id="title">{{title}}</h4> <p> 这是alert弹框 </p> <p> <button>取消</button> <button>确定</button> </p>
detail.component.ts建立模態框元件,我們需要建立一個元件,然後由ngx-bootstrap-model 幫忙引導啟動
對於這個元件需要繼承DialogComponent
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> implements AlertModel { title: string; message: string; constructor(dialogService: DialogService) { super(dialogService); } }</alertmodel>
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.component.html創建一個可以彈出模態框的按鈕
<p> </p><p> <button>modal模态框</button> </p>
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) => { }); } }
confirm.component.html編寫confirm彈框的佈局
<p> </p><p> </p><p> <button>×</button> </p><h4 id="title">{{title}}</h4> <p> 这是confirm弹框 </p> <p> <button>取消</button> <button>确定</button> </p>
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> 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); } }</confirmmodel>
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.component.html很簡單,就一個按鈕
<p> </p><p> <button>内置</button> </p>
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 }) } }</builtinoptions>
二、彈出方式二(此方法來自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.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方法把它显示出来 }; }</any>
app.component.html
<p> </p><p> <button>第二种弹出方式</button> </p> <!--第二种弹出方法的组件--> <template> <p> </p> <h4 id="第二种模态框">第二种模态框</h4> <button> <span>×</span> </button> <p> </p> <p><span>第二种模态框弹出方式</span></p> <p> <button>确定</button> <button>取消</button> </p> </template>
三、最終效果
我們將上面所有的彈框全部寫在一起,然後效果就是這樣的
相關推薦:
bootstrap模態框嵌套、tabindex屬性、去除陰影的方法
詳解bootstrap3-dialog-master模態方塊用法
以上是兩種Angular彈出模態框的方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

JavaScript是現代網站的核心,因為它增強了網頁的交互性和動態性。 1)它允許在不刷新頁面的情況下改變內容,2)通過DOMAPI操作網頁,3)支持複雜的交互效果如動畫和拖放,4)優化性能和最佳實踐提高用戶體驗。

C 和JavaScript通過WebAssembly實現互操作性。 1)C 代碼編譯成WebAssembly模塊,引入到JavaScript環境中,增強計算能力。 2)在遊戲開發中,C 處理物理引擎和圖形渲染,JavaScript負責遊戲邏輯和用戶界面。

JavaScript在網站、移動應用、桌面應用和服務器端編程中均有廣泛應用。 1)在網站開發中,JavaScript與HTML、CSS一起操作DOM,實現動態效果,並支持如jQuery、React等框架。 2)通過ReactNative和Ionic,JavaScript用於開發跨平台移動應用。 3)Electron框架使JavaScript能構建桌面應用。 4)Node.js讓JavaScript在服務器端運行,支持高並發請求。

Python更適合數據科學和自動化,JavaScript更適合前端和全棧開發。 1.Python在數據科學和機器學習中表現出色,使用NumPy、Pandas等庫進行數據處理和建模。 2.Python在自動化和腳本編寫方面簡潔高效。 3.JavaScript在前端開發中不可或缺,用於構建動態網頁和單頁面應用。 4.JavaScript通過Node.js在後端開發中發揮作用,支持全棧開發。

C和C 在JavaScript引擎中扮演了至关重要的角色,主要用于实现解释器和JIT编译器。1)C 用于解析JavaScript源码并生成抽象语法树。2)C 负责生成和执行字节码。3)C 实现JIT编译器,在运行时优化和编译热点代码,显著提高JavaScript的执行效率。

JavaScript在現實世界中的應用包括前端和後端開發。 1)通過構建TODO列表應用展示前端應用,涉及DOM操作和事件處理。 2)通過Node.js和Express構建RESTfulAPI展示後端應用。

JavaScript在Web開發中的主要用途包括客戶端交互、表單驗證和異步通信。 1)通過DOM操作實現動態內容更新和用戶交互;2)在用戶提交數據前進行客戶端驗證,提高用戶體驗;3)通過AJAX技術實現與服務器的無刷新通信。

理解JavaScript引擎內部工作原理對開發者重要,因為它能幫助編寫更高效的代碼並理解性能瓶頸和優化策略。 1)引擎的工作流程包括解析、編譯和執行三個階段;2)執行過程中,引擎會進行動態優化,如內聯緩存和隱藏類;3)最佳實踐包括避免全局變量、優化循環、使用const和let,以及避免過度使用閉包。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。