欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular6 寫一個簡單的Select組件示例

 更新時間:2018年08月20日 09:34:54   作者:niccky  
這篇文章主要介紹了Angular6寫一個簡單的Select組件示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

Select組件目錄結(jié)構

/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 組件

結(jié)構

/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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制)

    淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制)

    本篇文章主要介紹了淺談AngularJs 雙向綁定原理(數(shù)據(jù)綁定機制),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Bootstrap和Angularjs配合自制彈框的實例代碼

    Bootstrap和Angularjs配合自制彈框的實例代碼

    今天小編通過本文給大家分享Bootstrap和Angularjs配合自制彈框的實例代碼,代碼簡單易懂,有需要的朋友跟著小編一起學習
    2016-08-08
  • Angular 獨立組件入門指南

    Angular 獨立組件入門指南

    這篇文章主要為大家介紹了Angular 獨立組件入門教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-03-03
  • angular實現(xiàn)spa單頁面應用實例

    angular實現(xiàn)spa單頁面應用實例

    本篇文章主要介紹了angular實現(xiàn)spa單頁面應用實例,小本篇文章是對單頁面的一個簡單的基本邏輯操作,這個方法可以搭建基本的單頁面的邏輯結(jié)構。一起跟隨小編過來看看吧
    2017-07-07
  • 淺談AngularJS中使用$resource(已更新)

    淺談AngularJS中使用$resource(已更新)

    這篇文章主要介紹了淺談AngularJS中使用$resource(已更新)的相關資料,需要的朋友可以參考下
    2017-09-09
  • 基于AngularJS+HTML+Groovy實現(xiàn)登錄功能

    基于AngularJS+HTML+Groovy實現(xiàn)登錄功能

    AngularJS是一款客戶端MVC的javascript框架,而客戶端MVC代表未來架構(為什么要使用MVC+REST+CQRS架構),如果你有Struts或SpringMVC等后端MVC框架編程經(jīng)驗,學習Angular會很快,基本是按照同一個MVC思路實現(xiàn)的,本文給大家介紹AngularJS+HTML+Groovy實現(xiàn)登錄功能
    2016-02-02
  • Angular短信模板校驗代碼

    Angular短信模板校驗代碼

    這篇文章主要介紹了Angular短信模板校驗代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • Angular 中 select指令用法詳解

    Angular 中 select指令用法詳解

    這篇文章主要介紹了Angular 中 select指令用法詳解的相關資料,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下
    2016-09-09
  • 簡單實現(xiàn)AngularJS輪播圖效果

    簡單實現(xiàn)AngularJS輪播圖效果

    這篇文章主要教大家如何簡單實現(xiàn)AngularJS輪播圖效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02
  • ionic+AngularJs實現(xiàn)獲取驗證碼倒計時按鈕

    ionic+AngularJs實現(xiàn)獲取驗證碼倒計時按鈕

    本篇文章主要介紹了ionic+AngularJs實現(xiàn)獲取驗證碼倒計時按鈕,具有一定的參考價值,有興趣的可以了解一下。
    2017-04-04

最新評論