Angular 2.0 实现的搜索框

样式么。。。抄的bootstrap-table的搜索框

<style>
  .form-control {
    font-family: "Microsoft YaHei", Arial;
    font-size: 12px;
    width: 240px;
    border: 1px solid #dddddd;
    background: #ffffff;
    outline: none;
    box-shadow: none;
    padding: 6px 30px 6px 8px;
  }

  .search {
  height: 30px;
  width: 210px;
  background-color: #f6f6f6;
  border: 1px solid #f0f0f0;
  border-radius: 3px;
  padding-right: 30px;
  font-family: inherit;
  position: relative;
  height: 30px;
  width: 240px;
  float:right;
  margin: 0px;
  }

  .search span {
  position: absolute;
  width: 16px;
  height: 16px;
  top:6px;
  right: 10px;
  background: url("../images/icon_search_mirror.png") no-repeat right 0;
  cursor: pointer;
  }

</style>

<div class="search">
  <input class="form-control" type="text" #term (keyup)="search(term.value)" >
  <span></span>
</div>

然后实现

import { Component, EventEmitter, Input ,Output, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';

@Component({
  moduleId: module.id,
  selector: 'search-directive',
  templateUrl:'search-directive.component.html'

})

export class SearchDirectiveComponent implements OnInit {
  @Input() placeholder:string;
  @Output() searchContent = new EventEmitter<Object>();

  searchTermStream = new Subject<string>();


  search(term:string) {
    this.searchTermStream.next(term);
  }

  getSearchStream() {
    let that = this;
    return that.searchTermStream
      .debounceTime(300)
      .distinctUntilChanged();
  }

  ngOnInit() {
    let that = this;

    that.getSearchStream().subscribe((res)=> {
      this.searchContent.emit(res);
    });

  }


}
使用的时候export组件,然后<search-directive (searchContent)="searchContentChange($event)"> </search-directive>

在JS代码中searchContentChange(data)处理用户在输入框输入的数据,默认的延时是300ms,开心~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值