Select組件目錄結(jié)構(gòu)
創(chuàng)新互聯(lián)認(rèn)為,企業(yè)網(wǎng)站是要賦予品質(zhì)、思維、人性,深入到用戶內(nèi)心的細(xì)膩情感,才能真正稱(chēng)得上企業(yè)網(wǎng)站。創(chuàng)新互聯(lián)根據(jù)每位用戶內(nèi)心最深的需求網(wǎng)站建設(shè)服務(wù),堅(jiān)實(shí)的設(shè)計(jì)執(zhí)行是品牌長(zhǎng)期視覺(jué)塑造的重要支持。
/src /app /select /select.ts /select.html /select.css /options.ts /index.ts
//select.ts import { Component, ContentChildren, ViewChild, Input, Output, EventEmitter, QueryList, HostListener } from '@angular/core'; import { NzOptionDirective } from './option'; @Component({ selector: 'nz-select', templateUrl: './select.html', styleUrls: ['./select.css'] }) export class NzSelectComponent { @Input() isOpen: boolean; @Input() value: string; @Output() valueChange = new EventEmitter<any>(); label: string; @ContentChildren(NzOptionDirective, { descendants: true }) options: QueryList<NzOptionDirective>; ngAfterContentInit() { this.options.forEach(option => { option.select.subscribe(() => { this.value = option.value; this.label = option.renderLabel(); this.options.map(r => r.isSelected = false); option.isSelected = true; this.valueChange.emit(option.value); }) setTimeout(() => { option.isSelected = option.value === this.value; if (option.isSelected) { this.label = option.renderLabel(); this.valueChange.emit(option.value); } }); }) } @HostListener('click') onClick() { this.isOpen = !this.isOpen; } }
//select.html <ng-content *ngIf="isOpen"></ng-content> <div *ngIf="!isOpen">{{label}}</div>
//select.css :host { display: inline-block; border: 1px solid; cursor: pointer; text-align: center; border-radius: 4px; } :host .current{ padding:5px 10px; background:red; color:#FFF; text-align: center; width:40px; outline: none; border: none; } ::ng-deep div:not(.current):hover{ background:green; color:#FFF; } ::ng-deep .selected { font-weight: 700; background: red; color:#FFF; }
//options.ts import { Directive,HostBinding,HostListener,Input,Output,ElementRef,EventEmitter} from '@angular/core'; @Directive({ selector:'[nzOption]' }) export class NzOptionDirective{ @Input() value:string; constructor(private el:ElementRef){} @Output() select = new EventEmitter<any>(); @HostBinding("class.selected") isSelected: boolean; renderLabel(){ return (this.el.nativeElement.textContent || "").trim(); } @HostListener('click') onClick(){ this.select.emit(); } }
//index.ts import { NzOptionDirective } from './option'; import { NzSelectComponent } from './select'; export const components = [ NzSelectComponent, NzOptionDirective ];
應(yīng)用 Select 組件
結(jié)構(gòu)
/src /app/ /app.module.ts /app.component.ts /app.component.html
//app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { CommonModule } from '@angular/common'; import {components} from './select'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule, FormsModule,CommonModule ], declarations: [ AppComponent,...components], bootstrap: [ AppComponent ] }) export class AppModule { }
//app.component.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { name = 'Angular'; defaultValue: any = 'value5' menus: any[] = []; ngOnInit() { for (let i = 0; i <= 6; i++) { this.menus.push({ value: 'value' + i, label: 'item' + i }) } } }
//app.component.html <nz-select [(value)]="defaultValue" [isOpen]="false"> <div nzOption *ngFor="let m of menus" [value]="m.value">{{m.label}}</div> </nz-select> <pre> select value is <b>{{defaultValue|json}}</b> </pre>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文名稱(chēng):Angular6寫(xiě)一個(gè)簡(jiǎn)單的Select組件示例
路徑分享:http://jinyejixie.com/article46/peoeeg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、靜態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、ChatGPT、網(wǎng)站維護(hù)、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)