Angular PrimeNG Form InputNumber 前缀和后缀组件
Angular PrimeNG是一个开源框架,拥有丰富的原生Angular UI组件,可用于出色的造型,该框架可用于制作响应式网站,非常方便。在这篇文章中,我们将学习如何在Angular PrimeNG中使用**Form InputNumber Prefix and Suffix Component。我们还将学习这些属性,以及在代码中使用的语法。
InputNumber组件允许用户将内容输入为一个数值。通过使用prefix和suffix属性,自定义文本,如单位,可以被放置在输入部分之前或之后。
语法:
<p-inputNumber
placeholder="..."
prefix="..."
suffix="...">
</p-inputNumber>
Angular PrimeNG Form InputNumber Prefix and Suffix组件属性:
- placeholder。它用于设置输入时显示的咨询信息。它是字符串数据类型,默认值为空。
- prefix。它用于设置在数值前显示的文本。它是字符串数据类型,默认值为空。
- suffix。它用于设置在数值后面显示的文本。它是字符串数据类型,默认值是小数。
创建Angular应用程序和模块安装。
第1步:使用以下命令创建一个Angular应用程序。
ng new appname
第2步:创建你的项目文件夹即appname后,使用以下命令移动到它。
cd appname
第3步:在你给定的目录中安装PrimeNG。
npm install primeng --save
npm install primeicons --save
项目结构:安装成功后会出现以下项目结构。
- 运行下面的命令可以看到输出。
ng serve --save
示例1:在下面的代码中,我们将看到Angular PrimeNG Form InputNumber Prefix and Suffix Component使用prefix=”$”的演示。
- app.component.html:
<h1 style="color: green">GeeksforGeeks</h1>
<h5>
Angular PrimeNG Form InputNumber
Prefix and Suffix Component
</h5>
<p-inputNumber placeholder="Input any number"
prefix="$">
</p-inputNumber>
- app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent { }
- app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { InputNumberModule }
from 'primeng/inputnumber';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
InputNumberModule,
FormsModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出:
例子2:下面是另一个代码例子,演示了Angular PrimeNG Form InputNumber Prefix and Suffix Component使用suffix=” kg”的情况。
- app.component.html:
<h1 style="color: green">GeeksforGeeks</h1>
<h5>
Angular PrimeNG Form InputNumber
Prefix and Suffix Component
</h5>
<p-inputNumber placeholder="Input any number"
suffix=" kg">
</p-inputNumber>
- app.component.ts:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent { }
- app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule }
from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { BrowserAnimationsModule }
from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { InputNumberModule }
from 'primeng/inputnumber';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
InputNumberModule,
FormsModule
],
declarations: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
输出: