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 相关文章推荐
JavaScript 输入框内容格式验证代码
Feb 11 Javascript
JavaScript的类型简单说明
Sep 03 Javascript
js下将字符串当函数执行的方法
Jul 13 Javascript
JavaScript返回上一页的三种方法及区别介绍
Jul 04 Javascript
基于javascript代码检测访问网页的浏览器呈现引擎、平台、Windows操作系统、移动设备和游戏系统
Dec 03 Javascript
javascript实现网页端解压并查看zip文件
Dec 15 Javascript
JS制作适用于手机和电脑的通知信息效果
Oct 28 Javascript
微信小程序 UI与容器组件总结
Feb 21 Javascript
浅析vue component 组件使用
Mar 06 Javascript
使用vue-cli创建项目的图文教程(新手入门篇)
May 02 Javascript
JavaScript中的各种宽高属性的实现
May 08 Javascript
使用JS实现简易计算器
Jun 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
php中的一个中文字符串截取函数
2007/02/14 PHP
PHP错误提示的关闭方法详解
2013/06/23 PHP
迅速确定php多维数组的深度的方法
2014/01/07 PHP
PHP使用SOAP调用API操作示例
2018/12/25 PHP
Yii框架的路由配置方法分析
2019/09/09 PHP
jquery之Document元素选择器篇
2008/08/14 Javascript
获取服务器传来的数据 用JS去空格的正则表达式
2012/03/26 Javascript
jquery 按钮状态效果 正常、移上、按下
2013/08/12 Javascript
JS 屏蔽键盘不可用与鼠标右键不可用的方法
2013/11/18 Javascript
javascript面向对象特性代码实例
2014/06/12 Javascript
Jquery解析Json格式数据过程代码
2014/10/17 Javascript
jQuery插件fullPage.js实现全屏滚动效果
2016/12/02 Javascript
jQuery实现加入收藏夹功能(主流浏览器兼职)
2016/12/24 Javascript
详解webpack进阶之loader篇
2017/08/23 Javascript
代码详解javascript模块加载器
2018/03/04 Javascript
一些可能会用到的Node.js面试题
2019/06/15 Javascript
[02:15]2015国际邀请赛选手档案IG.Ferrari 430
2015/07/30 DOTA
Python中用max()方法求最大值的介绍
2015/05/15 Python
Python3 操作符重载方法示例
2017/11/23 Python
pandas 把数据写入txt文件每行固定写入一定数量的值方法
2018/12/28 Python
python3使用QQ邮箱发送邮件
2020/05/20 Python
python实现抽奖小程序
2020/04/15 Python
python实现小世界网络生成
2019/11/21 Python
Python yield生成器和return对比代码实例
2020/04/20 Python
Python接口测试结果集实现封装比较
2020/05/01 Python
西班牙拥有最佳品牌的动物商店:Animalear.com
2018/01/05 全球购物
澳大利亚现代波西米亚风格女装网站:Bohemian Traders
2018/04/16 全球购物
德国二手设计师时装和复古时装跳蚤市场:Mädchenflohmarkt
2020/11/09 全球购物
C#笔试题
2015/07/14 面试题
最新的大学生找工作自我评价
2013/09/29 职场文书
综合实践活动方案
2014/02/14 职场文书
后勤服务中心总经理工作职责
2014/03/03 职场文书
党章培训心得体会
2014/09/04 职场文书
2016年大学迎新工作总结
2015/10/14 职场文书
python四种出行路线规划的实现
2021/06/23 Python
Vue3.0中Ref与Reactive的区别示例详析
2021/07/07 Vue.js