Angular PrimeNG Menubar自定义内容

Angular PrimeNG Menubar自定义内容

Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,该框架可用于制作响应式网站,非常方便。在这篇文章中,我们将了解如何在Angular PrimeNG中使用Menubar自定义内容。我们还将学习属性,以及在代码中使用的语法。

Menubar组件用于制作一个水平条形式的菜单列表。

Angular PrimeNG Menubar自定义内容属性:

  • model。它是一个菜单项的数组。它接受数组数据类型作为输入,默认值为空。

语法:

<p-menubar [model]="...">
    ...
</p-menubar>

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

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

ng new appname

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

cd appname

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

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

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

Angular PrimeNG Menubar自定义内容

要运行上述文件,请运行以下命令:

ng serve --save

例子1:下面的例子说明了Angular PrimeNG Menubar自定义内容使用search和button.

  • app.component.html:
<h2 style="color: green">
    GeeksforGeeks
</h2>
<h5>
    PrimeNG Menubar Custom Content
</h5>
  
<p-menubar [model]="gfg">
    <input type="text" 
        pInputText placeholder="type here..." />
    <button pButton label="Log In">
    </button>
</p-menubar>
  • app.component.ts:
import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html'
})
  
export class AppComponent {
    gfg: MenuItem[];
  
    ngOnInit() {
        this.gfg = [
            {
                label: 'HTML',
                items: [
                    {
                        label: 'HTML 1'
                    },
                    {
                        label: 'HTML 2'
                    }
                ]
            },
            {
                label: 'Angular',
  
                items: [
                    {
                        label: 'Angular 1'
                    },
                    {
                        label: 'Angular 2'
                    }
                ]
            }
        ];
    }
}
  • app.component.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { MenubarModule } from 'primeng/menubar';
import { InputTextModule } from 'primeng/inputtext';
import { ButtonModule } from 'primeng/button';
  
@NgModule({
    imports: [
        BrowserModule,
        BrowserAnimationsModule,
        MenubarModule,
        InputTextModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

输出:

Angular PrimeNG Menubar自定义内容

例子2:下面是另一个例子,说明了Angular PrimeNG Menubar自定义内容使用dropdown和button。

  • app.component.html:
<h2 style="color: green">
    GeeksforGeeks
</h2>
<h5>
    PrimeNG Menubar Custom Content
</h5>
  
<p-menubar [model]="gfg">
    <p-dropdown 
        [options]="gfgCourses" 
        optionLabel="name">
    </p-dropdown>
  
    <button pButton 
        label="Log In">
    </button>
</p-menubar>
  • app.component.ts:
import { Component } from '@angular/core';
import { MenuItem } from 'primeng/api';
  
interface Courses {
    name: string;
    code: string;
}
  
@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
})
  
export class AppComponent {
    gfg: MenuItem[];
    gfgCourses: Courses[];
  
    constructor() {
        this.gfgCourses = [
            { name: 'C++ STL', code: 'c++' },
            { name: 'DSA Self Paced', code: 'DSA' },
            { name: 'System Design', code: 'SD' },
        ];
    }
    ngOnInit() {
        this.gfg = [
            {
                label: 'HTML',
                items: [
                    {
                        label: 'HTML 1',
                    },
                    {
                        label: 'HTML 2',
                    },
                ],
            },
            {
                label: 'Angular',
  
                items: [
                    {
                        label: 'Angular 1',
                    },
                    {
                        label: 'Angular 2',
                    },
                ],
            },
        ];
    }
}
  • app.component.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule }
    from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { DropdownModule } from 'primeng/dropdown';
import { MenubarModule } from 'primeng/menubar';
import { InputTextModule } from 'primeng/inputtext';
import { ButtonModule } from 'primeng/button';
import { FormsModule } from '@angular/forms';
  
@NgModule({
    imports: [
        DropdownModule,
        FormsModule,
        BrowserModule,
        BrowserAnimationsModule,
        MenubarModule,
        InputTextModule,
        ButtonModule
    ],
    declarations: [AppComponent],
    bootstrap: [AppComponent]
})
  
export class AppModule { }

输出:

Angular PrimeNG Menubar自定义内容

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程