Angular6 写一个简单的Select组件示例
网络编程 2021-07-04 16:46www.168986.cn编程入门
这篇文章主要介绍了Angular6写一个简单的Select组件示例,长沙网络推广觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随长沙网络推广过来看看吧
Select组件目录结构
/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 ponents = [ NzSelectComponent, NzOptionDirective ];
应用 Select 组件
结构
/src /app/ /app.module.ts /app.ponent.ts /app.ponent.html
//app.module.ts import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { FormsModule } from '@angular/forms'; import { CommonModule } from '@angular/mon'; import {ponents} from './select'; import { AppComponent } from './app.ponent'; @NgModule({ imports: [ BrowserModule, FormsModule,CommonModule ], declarations: [ AppComponent,...ponents], bootstrap: [ AppComponent ] }) export class AppModule { }
//app.ponent.ts import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.ponent.html', styleUrls: ['./app.ponent.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.ponent.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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程