This article mainly introduces the example of angular defining its own select component based on ng-alain. Now I share it with you and give it as a reference.
1. The first is the my-select2.component.html page, where new functions are added based on business needs based on ng-alain's select; the code is as follows:
<nz-select #select style="width:100%;" [(ngModel)]="selectedOption" [nzPlaceHolder]="myPlaceHolder" nzAllowClear [nzShowSearch]="true" [nzNotFoundContent]="'无匹配'"> <nz-option *ngFor="let option of options" [nzLabel]="option.label" [nzValue]="option" [nzDisabled]="option.disabled"> </nz-option> </nz-select>
2. Furthermore, it is the my-select2.component.ts page. There are comments in the code; the code is as follows:
import { ControlValueAccessor } from '@angular/forms/src/directives'; import { Component, forwardRef, Input,OnInit,ElementRef,Output,EventEmitter} from '@angular/core'; import { NG_VALUE_ACCESSOR } from '@angular/forms'; import { Router, NavigationEnd } from '@angular/router'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { SelectService } from './my-select2.service'; declare var $: any; @Component({ selector: 'nz-select2', templateUrl: './my-select2.component.html', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => NzSelect2Component),//注入表单控件 multi: true }] }) export class NzSelect2Component implements OnInit{ constructor(private selectService:SelectService) { } innerValue: any = ''; //监听绑定的值,与外岑的ngModel相互绑定 set selectedOption(val:any){ if (val !== this.innerValue) { this.innerValue = val; this.onChangeCallback(val.value); this.dataBack.emit(val.value); // 事件 } } get selectedOption():any{ return this.innerValue; } options = [];//接收select的数组 _dataSource:any;//接收本地的自定义数组或者请求返回的数组 @Input() url:any;//请求的url @Input() myPlaceHolder:any;//自定义的PlaceHolder @Input() //下拉框的数据格式 fieldKey:any = { text: 'text', value: 'value' }; @Input() set dataSource(val: any) { this._dataSource = val; if ($.isArray(this._dataSource)) { this.options=this._dataTransform(this._dataSource);//如果是本地数组或直接请求的数组直接复制 } } get dataSource(): any { return this._dataSource; } @Output() dataBack = new EventEmitter<any>(); registerOnChange(fn: (value: any) => void) { this.onChangeCallback = fn; } registerOnTouched(fn: any) { this.onTouchedCallback = fn; } writeValue(value: string) { } onChangeCallback = (value: any) => {}; onTouchedCallback = (value: any) => {}; ngOnInit() { //如果url存在则直接请求 if(this.url){ this.selectService.getValue(this.url).subscribe(data => { data = data.rows || data.data; this.options=this._dataTransform(data); }); } } //转换下拉框下的字段 _dataTransform(data: Array<any>){ let _data = []; for (let i = 0; i < data.length; i++) { _data[i] = {}; _data[i].label = data[i][this.fieldKey.text]; _data[i].value = data[i][this.fieldKey.value]; } return _data; } }
3. Then there is the my-select2.service.ts page, which mainly requests the drop-down array returned by the background interface. The url is the link passed by the parent component. The code is as follows:
import { Injectable } from '@angular/core'; import { Headers, Http, URLSearchParams,RequestOptions } from '@angular/http'; import { HttpClient, HttpErrorResponse } from '@angular/common/http'; import 'rxjs/add/operator/toPromise'; // import { environment } from '../../environments/environment'; @Injectable() export class SelectService { constructor(private http: HttpClient) {} getValue(url: any):any{ return this.http .get(url); } }
4. Then comes the myselect.module.ts page. Here, the prerequisite for using this component is to introduce import { NzSelectModule } from 'ng-zorro-antd', the code is as follows :
import { NgModule, ModuleWithProviders } from '@angular/core'; import { CommonModule } from '@angular/common'; import { FormsModule,ReactiveFormsModule } from '@angular/forms'; import { NzSelect2Component } from './my-select2.component'; import { SelectService } from './my-select2.service'; import { NzSelectModule } from 'ng-zorro-antd'; @NgModule({ imports: [ CommonModule, FormsModule, NzSelectModule, ReactiveFormsModule ], exports:[ NzSelect2Component ], declarations: [ NzSelect2Component ], providers: [ SelectService ] }) export class MySelectModule { constructor() { } }
5. To use, import the module you need: MySelectModule
import { MySelectModule } from 'bizapp/base/components/myselect/myselect.module';
6. How to call: url is the interface for requesting the background, and fieldKey is the format of the array. The fields here can be defined according to the format returned by the background. For example: the background return format is [{dmsm1:5,dmz: 5}]Then the definition of fieldKey is as follows. myPlaceHolder is the content displayed during initialization. If it is a local array, you only need to add [dataSource]="peer", where peer is the local array
<nz-select2 [url]="'analysis/api/data/code/list/030107'" [(ngModel)]="search2.hpzl" [fieldKey]="{text:'dmsm1',value:'dmz'}" [myPlaceHolder]="'号牌种类'"></nz-select2>
7, Summary: Through this component, we only need to modify the url and fieldKey to introduce and use it in any module, reducing the use of code and facilitating maintenance
The above is what I compiled for everyone, I hope it will be helpful to everyone in the future .
Related articles:
Detailed interpretation of Node timer knowledge
Detailed analysis of the Generator function in Es6
Use the Array filter() method to compress sparse arrays in JavaScript
The above is the detailed content of How to define your own select component based on ng-alain in angular?. For more information, please follow other related articles on the PHP Chinese website!