国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Angular6 寫一個簡單的Select組件示例

2024-05-06 15:31:22
字體:
來源:轉載
供稿:網友

Select組件目錄結構

/src  /app    /select    /select.ts    /select.html    /select.css    /options.ts    /index.ts
//select.tsimport { 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.tsimport { 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.tsimport { NzOptionDirective } from './option';import { NzSelectComponent } from './select';export const components = [ NzSelectComponent, NzOptionDirective];

應用 Select 組件

結構

/src  /app/    /app.module.ts    /app.component.ts    /app.component.html
//app.module.tsimport { 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 { }            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 霍林郭勒市| 衡阳市| 鄂伦春自治旗| 宜丰县| 清流县| 漾濞| 金秀| 黄梅县| 安福县| 冕宁县| 宁安市| 龙山县| 赤水市| 六安市| 莒南县| 剑阁县| 祁阳县| 绥德县| 蒙阴县| 聂拉木县| 雷州市| 四子王旗| 滕州市| 军事| 巴林左旗| 马龙县| 开原市| 利辛县| 卢龙县| 墨脱县| 靖安县| 玉山县| 玉门市| 庆云县| 永定县| 绥滨县| 库伦旗| 忻城县| 博罗县| 济宁市| 平罗县|