Angular PrimeNG侧边栏位置
Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,这个框架用于制作响应式网站,非常容易。在这篇文章中,我们将了解如何在Angular PrimeNG中使用侧边栏位置。我们还将了解在代码中使用的属性以及它们的语法。
侧边栏组件是用来制作一个重叠在屏幕边缘的元素。侧边栏的位置用来定义侧边栏的位置为左、右、顶、和底。默认的位置是left。
语法:
<p-sidebar
[(visible)]="..."
position="...">
.....
</p-sidebar>
<p-button
(click)="..."
label="...">
</p-button>
Angular PrimeNG侧边栏位置属性:
- visible:它指定了对话框的可见性。它是布尔数据类型,默认值为false。
- position。它指定了侧边栏的位置,有效值是”左”、”右”、”底”和”顶”,它是字符串数据类型,默认值是左。
创建Angular应用程序和模块安装。
第1步:使用以下命令创建一个Angular应用程序。
ng new appname
第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
第3步在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
项目结构:它将看起来像如下。
- 运行下面的命令可以看到输出。
ng serve --open
示例1:下面是说明使用top和bottom位置的Angular PrimeNG侧边栏位置的示例代码。
<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Sidebar Position</h5>
<p-sidebar
[(visible)]="gfg1"
styleClass="p-sidebar-md"
position="top">
<h2>This is a Top Sidebar!</h2>
<p-button
type="button"
(click)="gfg1 = false"
label="OK"
styleClass="p-button-info">
</p-button>
<p-button
type="button"
(click)="gfg1 = false"
label="Cancel"
styleClass="p-button-danger p-ml-2">
</p-button>
</p-sidebar>
<p-sidebar
[(visible)]="gfg2"
styleClass="p-sidebar-md"
position="bottom">
<h2>This is a Bottom Sidebar!</h2>
<p-button
type="button"
(click)="gfg2 = false"
label="OK"
styleClass="p-button-info">
</p-button>
<p-button
type="button"
(click)="gfg2 = false"
label="Cancel"
styleClass="p-button-danger p-ml-2">
</p-button>
</p-sidebar>
<p-button
(click)="gfg1 = true"
label="Position Top!"
styleClass="p-mr-2">
</p-button>
<p-button
(click)="gfg2 = true"
label="Position Bottom!">
</p-button>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss'],
})
export class AppComponent {}
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule }
from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { SidebarModule } from "primeng/sidebar";
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
SidebarModule,
ButtonModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
输出:
示例2:下面是另一个示例代码,说明了Angular PrimeNG侧边栏位置使用left和right位置。
<h1 style="color: green">GeeksforGeeks</h1>
<h5>Angular PrimeNG Sidebar Position</h5>
<p-sidebar
[(visible)]="gfg1"
styleClass="p-sidebar-md"
position="left">
<h2>This is a Left Sidebar!</h2>
<p-button
type="button"
(click)="gfg1 = false"
label="OK"
styleClass="p-button-info">
</p-button>
<p-button
type="button"
(click)="gfg1 = false"
label="Cancel"
styleClass="p-button-danger p-ml-2">
</p-button>
</p-sidebar>
<p-sidebar
[(visible)]="gfg2"
styleClass="p-sidebar-md"
position="right">
<h2>This is a Right Sidebar!</h2>
<p-button
type="button"
(click)="gfg2 = false"
label="OK"
styleClass="p-button-info">
</p-button>
<p-button
type="button"
(click)="gfg2 = false"
label="Cancel"
styleClass="p-button-danger p-ml-2">
</p-button>
</p-sidebar>
<p-button
(click)="gfg1 = true"
label="Position Left!"
styleClass="p-mr-2">
</p-button>
<p-button
(click)="gfg2 = true"
label="Position Right!">
</p-button>
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {}
import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule }
from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { ButtonModule } from "primeng/button";
import { SidebarModule } from "primeng/sidebar";
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
SidebarModule,
ButtonModule,
],
declarations: [AppComponent],
bootstrap: [AppComponent],
})
export class AppModule {}
输出: