小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

使用Angular和ng-bootstrap創(chuàng)建一個輸入地址

 碼農(nóng)9527 2021-04-28

 在本文中,您將逐步學(xué)習(xí)如何創(chuàng)建輸入地址組件。當我們想要節(jié)省空間并更好地在表單中組織輸入時,此功能很有用。

    用戶可以輕松地從一個位置讀取和操作地址字段。我們組件的最終輸出將如下圖所示:

web

    背景

    在開始之前,建議您滿足以下先決條件:

    VisualStudio程式碼

    有關(guān)使用Angular應(yīng)用程序進行開發(fā)的一些知識

    有關(guān)引導(dǎo)程序,typeScript和HTML的一些知識

    使用代碼

    開始之前,您需要:

    安裝AngularCLI。

    通過在CMD上運行以下命令行來創(chuàng)建新的應(yīng)用程序:

ng new dialog-address-form1復(fù)制代碼類型:[html]

    通過運行以下命令安裝最新版本的ng-bootstrap:

ng add @ng-bootstrap/ng-bootstrap1復(fù)制代碼類型:[html]

    安裝最新版本的“font-awesome”,以方便使用圖標:

npm install --save font-awesome1復(fù)制代碼類型:[html]

    A)編碼

    這個想法是關(guān)于創(chuàng)建一個可用作輸入表單的組件。此輸入顯示完整地址,并且可以通過模式進行編輯。

    此模式是由以下字段組成的形式:

    地址行1:必填字段,其中包含街道號和名稱

    地址行2:可選字段,其中包含有關(guān)您的位置的其他詳細信息

    城市:必填項

    郵政編碼:必填字段

    國家:必填項

    當用戶單擊“保存”按鈕時,結(jié)果將作為文本顯示在輸入字段中。

    要做到這一點:

    首先,創(chuàng)建Address實體:

export class Address{
  addressLine1: string;
  addressLine2: string;
  city: string;
  zipCode: number;
  country: string;

  /**

   * transform address object to string, it's useful to display data into input text.

   */

  public toStringFormat(){
 return `${this.addressLine1} ${this.addressLine2}, 
   ${this.zipCode} ${this.city}, ${this.country}`;
  }
}123456789101112131415161718復(fù)制代碼類型:[html]

    創(chuàng)建address表單組件:

ng generate component  address1復(fù)制代碼類型:[html]

    在“app/components”文件夾中創(chuàng)建“AddressComponent”:

<div class="modal-header">

  <h4 class="modal-title">Address form</h4>

  <button type="button" class="close" aria-label="Close" 
   (click)="activeModal.dismiss('exit click')">

 <span aria-hidden="true">×</span>
  </button></div><div class="modal-body">

  <form [formGroup]="formInstance">

 <div class="form-group">

   <label class="text-strong">address line 1 :</label>
   <input type="text" name="addressLine1" formControlName="addressLine1" 
 class="form-control form-control-sm">

 </div>

 <div class="form-group">

   <label class="text-strong">address line 2 :</label>

   <input type="text" name="addressLine2" formControlName="addressLine2" 
 class="form-control form-control-sm">

 </div>

 <div class="form-group">

   <label class="text-strong">zip code :</label>

   <input type="number" name="zipCode" formControlName="zipCode" 
 class="form-control form-control-sm" >

 </div>

 <div class="form-group">

   <label class="text-strong">city :</label>

   <input type="text" name="city" formControlName="city" 
 class="form-control form-control-sm">

 </div>

 <div class="form-group">

   <label class="text-strong">country :</label>

   <select  name="country" formControlName="country"  
 class="form-control form-control-sm" >

  <option value="">Choose a country</option>

  <option value="France">France</option>

  <option value="Germany">Germany</option>

  <option value="Italy">Italy</option>

   </select>

 </div>

  </form></div><div class="modal-footer">

  <button type="button" class="btn btn-outline-dark" 
  [disabled]="formInstance.invalid" (click)="save()">Save</button></div>12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879復(fù)制代碼類型:[html]

    修改'address.component.ts':

import { Component, OnInit } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { Address } from 'src/models/Address';

@Component({
  selector: 'app-address',
  templateUrl: './address.component.html',
  styleUrls: ['./address.component.scss']
})

export class AddressComponent {
  formInstance: FormGroup;

  constructor(public activeModal: NgbActiveModal) {
 this.formInstance = new FormGroup(
   {
  addressLine1: new FormControl('', Validators.required),
  addressLine2: new FormControl(''),
  city: new FormControl('', Validators.required),
  zipCode: new FormControl('', Validators.required),
  country: new FormControl('', Validators.required),
   }
 )
  }

  /**

   * this method close the active modal and pass the address object 
   * to parent component.

   */

  save(){
  this.activeModal.close
 (Object.assign(new Address(), this.formInstance.value));
  }
}1234567891011121314151617181920212223242526272829303132333435363738復(fù)制代碼類型:[html]

    修改app.module.ts':由于AddressComponent將動態(tài)創(chuàng)建并加載,因此不會將其引用到模板中。你應(yīng)該把它聲明為EntryComponents進入app.module.ts。

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { AddressComponent } from './components/address/address.component';
import { NgbModule } from '@ng-bootstrap/ng-bootstrap';
import { ReactiveFormsModule, FormsModule } from '@angular/forms';

@NgModule({

  imports: [
 BrowserModule,
 NgbModule,
 ReactiveFormsModule,
 FormsModule
  ],
  declarations: [
 AppComponent,
 AddressComponent
  ],
  entryComponents: [AddressComponent],
  providers: [],
  bootstrap: [AppComponent]
})

export class AppModule { }12345678910111213141516171819202122232425復(fù)制代碼類型:[html]

    最后,在表單中使用此組件:

    修改app.component.html:

    它聲明了一個新形式,該形式由“用戶名”的輸入文本和“地址”的另一個輸入文本組成,這些文本具有按鈕鏈接,允許從AddressComponent模式中顯示的''編輯地址字段。

<div class="flex-hcenter">
  <form style="max-width: 400px;">
 <div class="form-group">
   <label class="text-strong">User Name :</label>
   <input type="text" name="userName" class="form-control form-control-sm">
 </div>
 <div class="form-group">
   <label>Address :</label>
   <div class="input-group">
  <input type="text" class="form-control" name="addressInput" 
  [value]="inputAddressTextValue" readonly />
  <span class="glyphicon glyphicon-new-window"></span>
  <div class="input-group-append">
 <span class="input-group-text" (click)="openAddressModal()">
   <i class="fa fa-external-link"></i>
 </span>
  </div>
   </div>
 </div>
  </form></div>123456789101112131415161718192021復(fù)制代碼類型:[html]

    修改app.component.ts:

    在此組件中,我們NgbModal作為服務(wù)注入以能夠?qū)?AddressComponent'打開到模式中并等待結(jié)果。

import { Component, OnInit } from '@angular/core';
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap';
import { Address } from 'src/models/Address';
import { AddressComponent } from './components/address/address.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})

export class AppComponent implements OnInit {
  inputAddressTextValue = "";
  modalRef: NgbModalRef;

  constructor(private modalService: NgbModal) {
  }

  ngOnInit(): void {
  }

  /**

   * it shows an address component in modal, and waits for the modal to close
   * in order to display the result into the address field
   */

  openAddressModal() {

 const modalRef = this.modalService.open(AddressComponent, {
   backdrop: 'static',
   centered: true
 }).result.then((res: Address) => {
   this.inputAddressTextValue = res.toStringFormat();
 });
  }
}12345678910111213141516171819202122232425262728293031323334353637復(fù)制代碼類型:[html]

    示范

    首先,通過運行'npmstart'啟動Angular服務(wù)器。

    單擊地址鏈接以打開地址組件。

    填寫所有必填字段后,單擊“保存”按鈕。

    最后,輸入地址將保留最終結(jié)果。

web

    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多