国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當前位置: 首頁 - 科技 - 知識百科 - 正文

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

來源:懂視網 責編:小采 時間:2020-11-27 22:09:33
文檔

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

Angular6 寫一個簡單的Select組件示例:Select組件目錄結構 /src /app /select /select.ts /select.html /select.css /options.ts /index.ts //select.ts import { Component, ContentChildren, ViewChild, Input, Output, EventEmitt
推薦度:
導讀Angular6 寫一個簡單的Select組件示例:Select組件目錄結構 /src /app /select /select.ts /select.html /select.css /options.ts /index.ts //select.ts import { Component, ContentChildren, ViewChild, Input, Output, EventEmitt

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 components = [
 NzSelectComponent,
 NzOptionDirective
];

應用 Select 組件

結構

/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>

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

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

Angular6 寫一個簡單的Select組件示例:Select組件目錄結構 /src /app /select /select.ts /select.html /select.css /options.ts /index.ts //select.ts import { Component, ContentChildren, ViewChild, Input, Output, EventEmitt
推薦度:
標簽: 一個 示例 組件
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 亚洲欧美一区二区三区久久 | 精品一区二区三区的国产在线观看 | 国产全黄一级毛片 | 国产高清不卡一区二区三区 | 精品日韩一区二区 | 久久一 | 亚洲一区二区三区久久精品 | 欧美com| 日本韩国欧美一区 | 午夜看一级特黄a大片黑 | 另类一区二区 | 国产一区亚洲二区三区毛片 | 国产超级乱淫片中文 | 伊人网中文字幕 | 黑人一区二区三区中文字幕 | 亚洲欧美在线免费观看 | 亚洲欧洲中文字幕 | 91一区二区在线观看精品 | 国产精品亚洲精品观看不卡 | 久久国产欧美日韩高清专区 | 亚洲 欧美 自拍 另类 欧美 | zozozo性欧美禽交3 | 成人毛片一区二区三区 | 精品久久久久久久一区二区手机版 | 成a人片亚洲日本久久 | 久久精品免费一区二区视 | 亚洲网站免费 | 欧美日韩精品 | 国产美女白丝袜精品_a不卡 | 亚洲专区欧美 | 99热只有精品一区二区 | 在线观看国产 | 亚洲欧美另类第一页 | 日韩欧美在线观看成人 | 国产a久久精品一区二区三区 | 高清一级片 | 一级一级一级毛片免费毛片 | 国产欧美另类第一页 | 91精品一区二区三区在线 | 亚洲国产欧美国产综合一区 | 97一区二区三区四区久久 |