时间:2021-05-28
Select组件目录结构
/src /app /select /select.ts /select.html /select.css /options.ts /index.ts//select.tsimport { 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.tsimport { 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.tsimport { 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.tsimport { 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.tsimport { 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>以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Angular6热加载配置方案,分享给大家,具体如下:示例ng版本如下:$ng--version__________/\________||______/___
本文实例为大家分享了angular6开发steps步骤条组件的实现代码,供大家参考,具体内容如下1.先展示步骤条效果2.使用angular命令快速创建组件ngg
Angular团队在angular6中,使得创建Angular第三方库变得更为简单。如果你以前尝试过操作,你会发现其实不是很简单!那么流程是什么呢?首页我们构建
之前一直用的antd的Select组件,但在有些端并不适用,而原生的select样式修改不灵活,遂产生自己写一个组件的想法。观察select组件:{this.v
前言:最近使用bootstrap组件的时候发现一个易用性问题,很多简单的组件初始化都需要在JS里面写很多的初始化代码,比如一个简单的select标签,因为仅仅只