Angular6 寫一個簡單的Select組件示例
更新時間:2018年08月20日 09:34:54 作者:niccky
這篇文章主要介紹了Angular6寫一個簡單的Select組件示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
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>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
angular動態(tài)刪除ng-repaeat添加的dom節(jié)點的方法
本篇文章主要介紹了angular動態(tài)刪除ng-repaeat添加的dom節(jié)點的方法,非常具有實用價值,需要的朋友可以參考下2017-07-07
Angular.JS判斷復選框checkbox是否選中并實時顯示
最近因為工作需要做了一個選擇標簽的功能,把一些標簽展示給用戶,用戶選擇自己喜歡的標簽,就類似我們在購物網站看到的那種過濾標簽似的,所以這篇文章就給大家介紹了Angular.JS判斷復選框checkbox是否選中并實時顯示的方法,下面來一起看看吧。2016-11-11
Angular7實現(xiàn)拖放Drag?Drop示例詳解
這篇文章主要介紹了Angular7實現(xiàn)拖放Drag?Drop示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Angular外部使用js調用Angular控制器中的函數(shù)方法或變量用法示例
這篇文章主要介紹了Angular外部使用js調用Angular控制器中的函數(shù)方法或變量用法,結合實例形式分析了Angular基于外部JS調用控制器中方法與變量的具體實現(xiàn)步驟與相關技巧,需要的朋友可以參考下2016-08-08
angularjs 的數(shù)據綁定實現(xiàn)原理
本篇文章主要介紹了angularjs 的數(shù)據綁定實現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07

