之前封成了个指令,本来想本模块自己用,后来别的同事也要用,干脆封装成一个模块,供外部调用
使用(加了一个属性)
<ky-search [disabled]="true" (searchContent)="searchContentChange($event)"> </ky-search>
html
<div class="tableSearch">
<input class="inputSearch" type="text" #term (keyup)="search(term.value)" placeholder="请输入需要搜索内容" required/>
<span (click)="delSearch()"></span>
</div>
less部分--这部分请穆兄指导了下,写的很精彩,学习了
.tableSearch {
width: 240px;
position: relative;
float: right;
}
.inputPrime {
border-radius: 2px;
padding-left: 6px;
height: 30px;
width: 240px;
font-size: 12px;
color: #666666;
}
.isPrimeForSpan {
position: absolute;
right: 10px;
top: 7px;
width: 16px;
height: 16px;
}
.inputSearchIcon(@icon){
background: url(@icon) no-repeat right 0;
cursor: pointer;
}
.tableSearch .inputSearch:extend(.inputPrime) {
border: 1px solid @stroke-gray;
background: @default-white;
&:disabled {
background: @fill-gray;
}
&:focus {
border: 1px solid @default-blue;
}
& + span:extend(.isPrimeForSpan){
.inputSearchIcon("images/icon_search_mirror.png");
}
&:valid + span:extend(.isPrimeForSpan){
.inputSearchIcon("images/icon_search_delete.png");
}
&:invalid:focus + span:extend(.isPrimeForSpan) {
.inputSearchIcon("images/icon_search_mirror.png");
}
&:valid:focus + span:hover:extend(.isPrimeForSpan) {
.inputSearchIcon("images/icon_search_delete_hover.png");
}
}
ts部分
import { Component, Output, EventEmitter, OnInit } from '@angular/core';
import { Subject } from 'rxjs/Subject';
@Component({
moduleId: module.id,
selector: 'ky-search',
styleUrls: ['./ky-search.css'],
templateUrl: './ky-search.html',
})
export class KySearchComponent implements OnInit {
//@Input() placeholder:string;
@Output() searchContent = new EventEmitter<Object>();
searchTermStream = new Subject<string>();
search(term:string) {
this.searchTermStream.next(term);
}
delSearch() {
this.searchTermStream.next('');
$('.inputSearch').val('');
}
getSearchStream() {
let that = this;
return that.searchTermStream
.debounceTime(300)
.distinctUntilChanged();
}
ngOnInit() {
let that = this;
that.getSearchStream().subscribe((res)=> {
this.searchContent.emit(res);
});
}
}