• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題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關(guān)鍵字專題關(guān)鍵字專題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組件示例

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

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

    Angular6 寫一個簡單的Select組件示例:Select組件目錄結(jié)構(gòu) /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組件目錄結(jié)構(gòu) /src /app /select /select.ts /select.html /select.css /options.ts /index.ts //select.ts import { Component, ContentChildren, ViewChild, Input, Output, EventEmitt

    Select組件目錄結(jié)構(gòu)

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

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

    文檔

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

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

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 国产伦精品免编号公布| 国产精品视频一区二区噜噜| 中日韩产精品1卡二卡三卡| 精品精品国产自在久久高清| 中文字幕精品亚洲无线码二区| 亚洲精品国产成人专区| 国产精品美脚玉足脚交欧美| 一本色道久久综合亚洲精品| 欧美一区二区精品| 伊人久久精品线影院| 中文字幕无码精品三级在线电影| 国产精品成人99久久久久| 国产精品嫩草影院一二三区入口| 久久青青草原精品国产| 亚洲精品成人在线| 欧美日韩在线精品一区二区三区激情综合 | 国产精品亚洲片在线| 亚洲综合av永久无码精品一区二区| 精品成人一区二区三区四区| 91精品福利在线观看| 久久精品视频免费| 国产欧美久久久精品| 8x福利精品第一导航| 精品人妻久久久久久888| 日韩精品一区二区亚洲AV观看| 亚洲国产精品视频| 欧美人与性动交α欧美精品成人色XXXX视频 | 精品国产污污免费网站入口在线| 日韩精品一区二区三区四区 | 国产精品偷窥熟女精品视频| 999精品色在线播放| 88久久精品无码一区二区毛片| 欧美成人精品一区二区三区| 久久精品国产久精国产| 国产精品欧美一区二区三区不卡 | 国产精品无码专区在线观看| 国产麻豆精品入口在线观看| 国产亚洲精品AA片在线观看不加载| 国产啪亚洲国产精品无码| 国产在线精品一区二区不卡麻豆| 精品国产午夜福利在线观看|