Angular PrimeNG对话框的初始焦点

Angular PrimeNG对话框的初始焦点

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,该框架可用于制作响应式网站,非常方便。在这篇文章中,我们将知道如何在Angular PrimeNG中使用Dialog Initial Focus.我们还将了解代码中将要使用的属性及其语法。

对话组件是用来做一个包含一些内容的组件,在一个叠加窗口中显示。当对话被显示时,一个具有自动对焦功能的元素会收到焦点。

Angular PrimeNG对话框的初始焦点属性:

  • header。它是对话框的标题文本。它是字符串数据类型,默认值为空。
  • position。它用于设置对话框的位置。它是字符串数据类型,默认值为中心。
  • visible。它指定了对话框的可见性。它是布尔数据类型,默认值为false。
  • styleClass。它用于设置组件的风格类。它是字符串数据类型,默认值为空。
  • autofocus。它用于在弹出对话框时对元素应用对焦。

语法:

<p-dialog 
       position="..." 
       header="...." 
       [(visible)]="...">
      <button 
              type="button" 
              pButton 
              autofocus>
      </button>
</p-dialog>

<p-button 
       (click)="..." 
       label="....">
</p-button>
HTML

创建Angular应用程序和模块安装。

第1步:使用以下命令创建一个Angular应用程序。

ng new appname
JavaScript

第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。

cd appname
JavaScript

步骤3:在你给定的目录中安装PrimeNG。

npm install primeng --save
npm install primeicons --save
JavaScript

项目结构:它将看起来像如下。

Angular PrimeNG对话框的初始焦点

  • 运行下面的命令可以看到输出。
ng serve --open
JavaScript

示例1:下面的代码示例说明了Angular PrimeNG Dialog Initial Focus使用autofocus属性的用法。

<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>PrimeNG Dialog Initial Focus</h5>
  
    <p-dialog position="center"
              header="GeeksforGeeks"
              [(visible)]="geeks">
        <button type="button" 
                pButton autofocus label="Autofocus">
        </button>
    </p-dialog>
  
    <p-button (click)="gfg()" 
              label="Click Here">
    </p-button>
</div>
HTML
import { Component } from "@angular/core";
import { PrimeNGConfig } from "primeng/api";
  
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  constructor(private primengConfig: PrimeNGConfig) {}
  
  ngOnInit() {
    this.primengConfig.ripple = true;
  }
  geeks: boolean;
  
  gfg() {
    this.geeks = true;
  }
}
JavaScript
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { DialogModule } from "primeng/dialog";
import { ButtonModule } from "primeng/button";
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
              DialogModule, 
              ButtonModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}
JavaScript

输出:

Angular PrimeNG对话框的初始焦点

例子2:下面是另一个代码例子,说明了Angular PrimeNG Dialog Initial Focus的使用,没有使用autofocus属性。

<div style="text-align: center">
    <h2 style="color: green">GeeksforGeeks</h2>
    <h5>PrimeNG Dialog Initial Focus</h5>
  
    <p-dialog position="center" 
              header="GeeksforGeeks"
              [(visible)]="geeks">
        <button type="button" 
                pButton label="Without autofocus">
        </button>
    </p-dialog>
  
    <p-button (click)="gfg()"
              label="Click Here">
    </p-button>
</div>
HTML
import { Component } from "@angular/core";
import { PrimeNGConfig } from "primeng/api";
  
@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
})
export class AppComponent {
  constructor(private primengConfig: PrimeNGConfig) {}
  
  ngOnInit() {
    this.primengConfig.ripple = true;
  }
  geeks: boolean;
  
  gfg() {
    this.geeks = true;
  }
}
JavaScript
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } 
    from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { DialogModule } from "primeng/dialog";
import { ButtonModule } from "primeng/button";
  
@NgModule({
  imports: [BrowserModule, 
              BrowserAnimationsModule, 
              DialogModule, 
              ButtonModule],
  declarations: [AppComponent],
  bootstrap: [AppComponent],
})
export class AppModule {}
JavaScript

输出:

Angular PrimeNG对话框的初始焦点

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册