Angular6 写一个简单的Select组件示例


Posted in Javascript onAugust 20, 2018

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
驱动事件的addEvent.js代码
Mar 27 Javascript
让GoogleCode的SVN下的HTML文件在FireFox下正常显示.
May 25 Javascript
Javascript学习笔记7 原型链的原理
Jan 11 Javascript
JavaScript学习笔记(十)
Jan 17 Javascript
jQuery easyui datagrid动态查询数据实例讲解
Feb 26 Javascript
JS实现超简洁网页title标题跑动闪烁提示效果代码
Oct 23 Javascript
vue路由跳转时判断用户是否登录功能的实现
Oct 26 Javascript
js中如何完美的解析数据
Mar 18 Javascript
Vue+Webpack完美整合富文本编辑器TinyMce的方法
Nov 30 Javascript
详解关于html,css,js三者的加载顺序问题
Apr 10 Javascript
微信小程序canvas开发水果老虎机的思路详解
Feb 07 Javascript
解决vant中 tab栏遇到的坑 van-tabs
Nov 04 Javascript
Layer弹出层动态获取数据的方法
Aug 20 #Javascript
layui 给数据表格加序号的方法
Aug 20 #Javascript
解决LayUI表单获取不到data的问题
Aug 20 #Javascript
Layui数据表格之获取表格中所有的数据方法
Aug 20 #Javascript
解决layui上传文件提示上传异常,实际文件已经上传成功的问题
Aug 19 #Javascript
解决layui中table异步数据请求不支持自定义返回数据格式的问题
Aug 19 #Javascript
Vue常用指令详解分析
Aug 19 #Javascript
You might like
我的php学习笔记(毕业设计)
2012/02/21 PHP
php微信公众开发之获取周边酒店信息的方法
2014/12/22 PHP
PHP数据分析引擎计算余弦相似度算法示例
2017/08/08 PHP
在JavaScript中通过URL传递汉字的方法
2007/04/09 Javascript
javascript实现tabs选项卡切换效果(扩展版)
2013/03/19 Javascript
简介JavaScript中Boolean.toSource()方法的使用
2015/06/05 Javascript
Vue.js基础知识汇总
2016/04/27 Javascript
基于Bootstrap实现图片轮播效果
2016/05/22 Javascript
jQueryUI 拖放排序遇到滚动条时有可能无法执行排序的小bug及解决方案
2016/12/19 Javascript
关于axios返回空对象的问题解决
2017/04/04 Javascript
从零开始学习Node.js系列教程之设置HTTP头的方法示例
2017/04/13 Javascript
Vue.js中关于侦听器(watch)的高级用法示例
2018/05/02 Javascript
webpack里使用jquery.mCustomScrollbar插件的方法
2018/05/30 jQuery
详解js模板引擎art template数组渲染的方法
2018/10/09 Javascript
layui 弹出删除确认界面的实例
2019/09/06 Javascript
基于JavaScript实现轮播图效果
2021/01/02 Javascript
python设置windows桌面壁纸的实现代码
2013/01/28 Python
Django中的Signal代码详解
2018/02/05 Python
Python装饰器结合递归原理解析
2020/07/02 Python
详解Selenium-webdriver绕开反爬虫机制的4种方法
2020/10/28 Python
python中uuid模块实例浅析
2020/12/29 Python
海滩咖啡馆:Beach Cafe
2018/02/02 全球购物
意大利制造的西装、衬衫和针对男士量身定制的服装:Lanieri
2018/04/08 全球购物
澳大利亚儿童鞋在线:The Trybe
2019/07/16 全球购物
中软国际Java程序员笔试题
2014/07/19 面试题
英文版餐饮业求职信
2013/10/18 职场文书
财务副总经理工作职责
2013/11/25 职场文书
幼儿园教师工作感言
2014/02/15 职场文书
党员群众路线剖析材料
2014/10/08 职场文书
2014年财务个人工作总结
2014/12/08 职场文书
会议欢迎词范文
2015/01/27 职场文书
新闻稿格式范文
2015/07/18 职场文书
预备党员的思想汇报,你真的会写吗?
2019/06/28 职场文书
goland 恢复已更改文件的操作
2021/04/28 Golang
Python 线程池模块之多线程操作代码
2021/05/20 Python
实操Python爬取觅知网素材图片示例
2021/11/27 Python