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 相关文章推荐
js控制表单操作的常用代码小结
Aug 15 Javascript
ExtJs纵坐标值重复问题的解决方法
Feb 27 Javascript
JS实现的表头列头固定页面功能示例
Jan 10 Javascript
微信小程序 轮播图swiper详解及实例(源码下载)
Jan 11 Javascript
Vue.js之slot深度复制详解
Mar 10 Javascript
vue中用H5实现文件上传的方法实例代码
May 27 Javascript
bootstrap插件treeview实现全选父节点下所有子节点和反选功能
Jul 21 Javascript
AngularJS使用ui-route实现多层嵌套路由的示例
Jan 10 Javascript
jQuery 改变P标签文本值方法
Feb 24 jQuery
nuxt中使用路由守卫的方法步骤
Jan 27 Javascript
微信小程序基于高德地图查找位置并显示文字
Oct 30 Javascript
Vue SPA 初次进入加载动画实现代码
Nov 14 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
怎样在UNIX系统下安装php3
2006/10/09 PHP
php ios推送(代码)
2013/07/01 PHP
html静态页面调用php文件的方法
2014/11/13 PHP
PHP的Socket网络编程入门指引
2015/08/11 PHP
php导出csv文件,可导出前导0实例代码
2016/11/16 PHP
可以显示单图片,多图片ajax请求的ThickBox3.1类下载
2007/12/23 Javascript
jquery中的sortable排序之后的保存状态的解决方法
2010/01/28 Javascript
引用 js在IE与FF之间的区别详细解析
2013/11/20 Javascript
排序算法的javascript实现与讲解(99js手记)
2014/09/28 Javascript
Nodejs实现多人同时在线移动鼠标的小游戏分享
2014/12/06 NodeJs
jQuery实现点击后标记当前菜单位置(背景高亮菜单)效果
2015/08/22 Javascript
Bootstrap每天必学之导航组件
2016/04/25 Javascript
Node.js中路径处理模块path详解
2016/11/14 Javascript
深入理解javascript中concat方法
2016/12/12 Javascript
前端分页功能的实现以及原理(jQuery)
2017/01/22 Javascript
js实现百度搜索提示框
2017/02/05 Javascript
vue2.0父子组件间通信的实现方法
2017/04/19 Javascript
Vue.js数据绑定之data属性
2017/07/07 Javascript
浅谈Angular2 模块懒加载的方法
2017/10/04 Javascript
VUE前端cookie简单操作
2017/10/17 Javascript
ES6基础之字符串和函数的拓展详解
2019/08/22 Javascript
用Node写一条配置环境的指令
2019/11/14 Javascript
简单使用Python自动生成文章
2014/12/25 Python
Python selenium抓取微博内容的示例代码
2018/05/17 Python
python使用socket实现的传输demo示例【基于TCP协议】
2019/09/24 Python
学会python自动收发邮件 代替你问候女友
2020/05/20 Python
Django配置Bootstrap, js实现过程详解
2020/10/13 Python
HTML5 canvas实现雪花飘落特效
2016/03/08 HTML / CSS
英国护发和美妆在线商店:Klip Shop
2019/03/24 全球购物
Yahoo的PHP面试题
2014/05/26 面试题
Eclipse面试题
2014/03/22 面试题
自荐信的格式
2014/03/10 职场文书
校长寄语大全
2014/04/09 职场文书
党支部审查意见
2015/06/02 职场文书
浅谈Python numpy创建空数组的问题
2021/05/25 Python
关于React Native使用axios进行网络请求的方法
2021/08/02 Javascript