<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答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
        主站蜘蛛池模板: 亚洲AV乱码一区二区三区林ゆな | 成年女人免费碰碰视频| 亚洲AV乱码一区二区三区林ゆな| 中文字幕成人免费高清在线视频| 亚洲国产精品专区在线观看| 污污免费在线观看| 亚洲AV无码成H人在线观看| 深夜福利在线视频免费| 亚洲?v女人的天堂在线观看| 美女扒开屁股让男人桶爽免费| 天堂亚洲免费视频| 在线视频网址免费播放| 亚洲精品无码mv在线观看网站| 日本免费人成网ww555在线| 亚洲美女大bbbbbbbbb| 岛国av无码免费无禁网站| 亚洲色欲色欲www在线播放| 国产一区二区三区免费在线观看| 色吊丝性永久免费看码| 在线观看国产区亚洲一区成人| 美女巨胸喷奶水视频www免费| 亚洲av无码一区二区三区不卡 | 国产亚洲午夜高清国产拍精品| 国产又黄又爽又大的免费视频| 久久亚洲精精品中文字幕| **一级毛片免费完整视| 亚洲色成人WWW永久在线观看| 国产不卡免费视频| 久久er国产精品免费观看2| 亚洲理论片在线中文字幕| 成人永久免费高清| 日韩免费观看一区| 亚洲日韩国产一区二区三区在线| 午夜国产羞羞视频免费网站| 免费成人在线电影| 久久亚洲精品国产亚洲老地址 | 亚洲国产精品va在线播放| 日本亚洲免费无线码| 亚洲AV成人无码网站| 久久噜噜噜久久亚洲va久| 色吊丝永久在线观看最新免费|