Angular PrimenG - 复选框和提交功能
Angular PrimenG - Checkbox and Submit functionality
我创建了一个组件,它会根据用户使用 PrimeNg
选择的城市显示各个国家/地区。
如果用户选择 伦敦 复选框并点击提交,请求应转到后端 API 并获取国家/地区名称并在 UI 上显示为:所选城市在英国。
后端JSON响应:
data: {
"country" : "UK",
"status" : "success"
}
项目文件是:
- app.component.html
<h5>Select City</h5>
<div class="p-field-checkbox">
<p-checkbox name="group1" value="New York" [(ngModel)]="selectedCities" inputId="ny"></p-checkbox>
<label for="ny">New York</label>
</div>
<div class="p-field-checkbox">
<p-checkbox name="group1" value="San Francisco" [(ngModel)]="selectedCities" inputId="sf"></p-checkbox>
<label for="sf">London</label>
</div>
<p-button label="Submit"></p-button>
- app.component.ts
import { Component } from '@angular/core';
import {MenuItem} from 'primeng/api';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
selectedCities: string[] = [];
checked: boolean = false;
ngOnInit() {}
}
- app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { ButtonModule } from 'primeng/button';
import { CheckboxModule } from 'primeng/checkbox';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
CheckboxModule,
FormsModule,
ButtonModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
UI 如下所示:
由于我是 Angular 开发的新手,我不知道如何存储所选城市并显示相应的国家/地区。
非常感谢任何线索或帮助。
app.component.html
<h5>Select City</h5>
<ng-container *ngFor="let city of cities; let i = index">
<div class="p-field-radiobutton">
<p-radioButton name="city" [value]="city" [(ngModel)]="selectedCity" id="i" (onClick)="selectedCountry = null">
</p-radioButton>
<label for="i">{{ city }}</label>
</div>
</ng-container>
<br>
<p-button label="Submit" (onClick)="onSubmit()"></p-button>
<br>
<br>
<label *ngIf="selectedCountry">
Selected city is {{ selectedCity}}<br>Selected country is {{selectedCountry}}</label>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
selectedCity: string;
selectedCountry: string = null;
checked: boolean = false;
cities: string[] = [
'New York',
'London',
'Delhi',
'Mumbai',
'Chennai',
'Hongkong',
'Dhaka',
];
ngOnInit() {}
onSubmit() {
if (!this.selectedCity) {
alert('Please select a city');
return;
}
this.selectedCountry = this.getCountryName();
}
getCountryName() {
// make your api call here
// I am just mocking
let country;
switch(this.selectedCity) {
case 'New York':
country = 'USA';
break;
case 'London':
country = 'UK';
break;
case 'Delhi':
case 'Mumbai':
case 'Chennai':
country = 'India';
break;
case 'Hongkong':
country = 'China';
break;
case 'Dhaka':
country = 'Bangladesh';
break;
}
return country;
}
}
我已经在 Stackblitz 使用单选按钮实现了一个工作演示。
因为我不知道你的后端 API。因此,我为演示添加了预定义数据。
看看是否适合你。
我创建了一个组件,它会根据用户使用 PrimeNg
选择的城市显示各个国家/地区。
如果用户选择 伦敦 复选框并点击提交,请求应转到后端 API 并获取国家/地区名称并在 UI 上显示为:所选城市在英国。
后端JSON响应:
data: {
"country" : "UK",
"status" : "success"
}
项目文件是:
- app.component.html
<h5>Select City</h5>
<div class="p-field-checkbox">
<p-checkbox name="group1" value="New York" [(ngModel)]="selectedCities" inputId="ny"></p-checkbox>
<label for="ny">New York</label>
</div>
<div class="p-field-checkbox">
<p-checkbox name="group1" value="San Francisco" [(ngModel)]="selectedCities" inputId="sf"></p-checkbox>
<label for="sf">London</label>
</div>
<p-button label="Submit"></p-button>
- app.component.ts
import { Component } from '@angular/core';
import {MenuItem} from 'primeng/api';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
selectedCities: string[] = [];
checked: boolean = false;
ngOnInit() {}
}
- app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import {FormsModule} from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppComponent } from './app.component';
import { ButtonModule } from 'primeng/button';
import { CheckboxModule } from 'primeng/checkbox';
@NgModule({
imports: [
BrowserModule,
BrowserAnimationsModule,
CheckboxModule,
FormsModule,
ButtonModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
UI 如下所示:
由于我是 Angular 开发的新手,我不知道如何存储所选城市并显示相应的国家/地区。
非常感谢任何线索或帮助。
app.component.html
<h5>Select City</h5>
<ng-container *ngFor="let city of cities; let i = index">
<div class="p-field-radiobutton">
<p-radioButton name="city" [value]="city" [(ngModel)]="selectedCity" id="i" (onClick)="selectedCountry = null">
</p-radioButton>
<label for="i">{{ city }}</label>
</div>
</ng-container>
<br>
<p-button label="Submit" (onClick)="onSubmit()"></p-button>
<br>
<br>
<label *ngIf="selectedCountry">
Selected city is {{ selectedCity}}<br>Selected country is {{selectedCountry}}</label>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
selectedCity: string;
selectedCountry: string = null;
checked: boolean = false;
cities: string[] = [
'New York',
'London',
'Delhi',
'Mumbai',
'Chennai',
'Hongkong',
'Dhaka',
];
ngOnInit() {}
onSubmit() {
if (!this.selectedCity) {
alert('Please select a city');
return;
}
this.selectedCountry = this.getCountryName();
}
getCountryName() {
// make your api call here
// I am just mocking
let country;
switch(this.selectedCity) {
case 'New York':
country = 'USA';
break;
case 'London':
country = 'UK';
break;
case 'Delhi':
case 'Mumbai':
case 'Chennai':
country = 'India';
break;
case 'Hongkong':
country = 'China';
break;
case 'Dhaka':
country = 'Bangladesh';
break;
}
return country;
}
}
我已经在 Stackblitz 使用单选按钮实现了一个工作演示。
因为我不知道你的后端 API。因此,我为演示添加了预定义数据。
看看是否适合你。