directive:

import { Directive, HostListener, HostBinding, ElementRef } from '@angular/core';

@Directive({
selector: '[credit-card]'
})
export class CreditCardDirective { @HostBinding('style.border')
border: string; @HostListener('input', ['$event'])
onKeyDown(event: KeyboardEvent) { const input = event.target as HTMLInputElement; let trimmed = input.value.replace(/\s+/g, '');
if (trimmed.length > ) {
trimmed = trimmed.substr(, );
} let numbers = [];
for (let i = ; i < trimmed.length; i += ) {
numbers.push(trimmed.substr(i, ));
} input.value = numbers.join(' '); this.border = '';
if (/[^\d]+/.test(trimmed)) {
this.border = '1px solid red';
} }
}

test:

import { DebugElement, Component } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { BrowserDynamicTestingModule, platformBrowserDynamicTesting } from '@angular/platform-browser-dynamic/testing';
import { By } from '@angular/platform-browser';
import { CreditCardDirective } from './credit-card.directive'; TestBed.initTestEnvironment(
BrowserDynamicTestingModule,
platformBrowserDynamicTesting()
); @Component({
template: `
<input type="text" [value]="value" credit-card>
`
})
class TestComponent {
value = ;
} describe('CreditCardDirective', () => { let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let el: DebugElement; beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
CreditCardDirective,
TestComponent
]
});
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
el = fixture.debugElement;
}); it('should format the string with spaces', () => {
const directive = el.query(By.directive(CreditCardDirective)).nativeElement;
directive.value = '';
directive.dispatchEvent(new Event('input'));
expect(directive.value).toBe('4751 23');
directive.value = '';
directive.dispatchEvent(new Event('input'));
expect(directive.value).toBe('4751 2398 1201 9201');
}); it('should have a max-length of 16 characters', () => {
const directive = el.query(By.directive(CreditCardDirective));
const directiveEl = directive.nativeElement;
directiveEl.value = '';
directiveEl.dispatchEvent(new Event('input'));
expect(directiveEl.value).toBe('4751 2398 1201 9201');
directiveEl.value = 'abscdef';
directiveEl.dispatchEvent(new Event('input'));
const directiveInstance = directive.injector.get(CreditCardDirective);
expect(directiveInstance.border).toContain('1px solid red');
}); });

最新文章

  1. 学习ES6--data2
  2. centos6.6 安装 LXC
  3. win10上安装Docker
  4. js基础知识总结(全)
  5. (七)Angularjs - 控制器
  6. 【自学php】第三天 - 读写文件
  7. 小强的HTML5移动开发之路(50)——jquerymobile页面初始化过程
  8. centos7使用cobbler(2.8)批量部署操作系统之一
  9. iOS - XMPP Openfire 服务器的搭建
  10. python nonloacal
  11. ubuntu16.04下g++安装及使用
  12. Centos 7 Ntop 流量分析 安装
  13. WeUI移动页面实现时间选择器(年-月-日-时-分)
  14. Go语言实现数据结构(一)单链表
  15. myeclipse 代码提示
  16. Tensorflow從源碼編譯
  17. centos7 安装java和tomcat9
  18. linux 相关命令记录
  19. AJAX传递数组
  20. linux 虚拟机设置好固定ip 修改了 dns 网络需要设置成桥接模式

热门文章

  1. Visual Studio Code配置GoLang开发环境
  2. Http协议的断点续传下载器,使用观察者模式监视下载进度,使用xml保存下载进度。
  3. 洛谷 P2908 [USACO08OPEN]文字的力量Word Power
  4. HDU 1848(sg博弈) Fibonacci again and again
  5. Android: 分页浏览的利器 android View Pager
  6. 关于C++中的内存泄露
  7. python序列中是否包含某个元素
  8. vue 自定义分页组件
  9. BZOJ——T2190: [SDOI2008]仪仗队
  10. c++ builder firemonkey 实现填充椭圆