Angular6 寫一個簡單的Select組件示例
更新時間:2018年08月20日 09:34:54 作者:niccky
這篇文章主要介紹了Angular6寫一個簡單的Select組件示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制)
本篇文章主要介紹了淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12
Bootstrap和Angularjs配合自制彈框的實例代碼
今天小編通過本文給大家分享Bootstrap和Angularjs配合自制彈框的實例代碼,代碼簡單易懂,有需要的朋友跟著小編一起學(xué)習(xí)2016-08-08
angular實現(xiàn)spa單頁面應(yīng)用實例
本篇文章主要介紹了angular實現(xiàn)spa單頁面應(yīng)用實例,小本篇文章是對單頁面的一個簡單的基本邏輯操作,這個方法可以搭建基本的單頁面的邏輯結(jié)構(gòu)。一起跟隨小編過來看看吧2017-07-07
基于AngularJS+HTML+Groovy實現(xiàn)登錄功能
AngularJS是一款客戶端MVC的javascript框架,而客戶端MVC代表未來架構(gòu)(為什么要使用MVC+REST+CQRS架構(gòu)),如果你有Struts或SpringMVC等后端MVC框架編程經(jīng)驗,學(xué)習(xí)Angular會很快,基本是按照同一個MVC思路實現(xiàn)的,本文給大家介紹AngularJS+HTML+Groovy實現(xiàn)登錄功能2016-02-02
ionic+AngularJs實現(xiàn)獲取驗證碼倒計時按鈕
本篇文章主要介紹了ionic+AngularJs實現(xiàn)獲取驗證碼倒計時按鈕,具有一定的參考價值,有興趣的可以了解一下。2017-04-04

