angular inputNumber指令輸入框只能輸入數(shù)字的實(shí)現(xiàn)
1、建立一個(gè)獨(dú)立模塊用于作為公用指令的模塊
1)生成模塊
ng g m directive
2)進(jìn)入指令模塊目錄
cd directive
3)生成一個(gè)只能輸入數(shù)字的指令類
ng g d numberinput
4)指令模塊directive.module.ts代碼如下
import { NgModule, ModuleWithProviders } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NumberinputDirective } from './numberinput.directive';
@NgModule({
imports: [
CommonModule
],
declarations: [NumberinputDirective],
exports:[ // 使引用該模塊的類可以使用該指令
NumberinputDirective
]
})
export class DirectiveModule { }
5)指令類numberinput.directive.ts代碼如下
@Directive({
selector: 'input[numberInput]'
})
export class NumberInputDirective {
// tslint:disable-next-line: no-input-rename
@Input('numberInput') numberType: string;
constructor(private el: ElementRef) {}
@HostListener('input', ['$event.target.value'])
onChange(value: string): void {
if (this.numberType.length < 1) {
this.updateIntegerValue(value);
} else {
this.el.nativeElement.value = value.replace(/[^\d.]/g, '');
}
}
@HostListener('blur', ['$event.target.value']) onBlur(value: number): void {
if (this.numberType.length >= 1) {
this.updateFloatValue(value);
}
}
updateIntegerValue(value: string): void {
this.el.nativeElement.value = value.replace(/[^\d]/g, '');
}
updateFloatValue(floatValue: number): void {
const value = String(floatValue);
const reg = /^-?(0|[1-9][0-9]*)(\.[0-9]*)?$/.test(value);
const numBegin = /^\d/.test(value);
const numEnd = /\d$/.test(value);
if (reg && numBegin && numEnd) {
this.el.nativeElement.value = value;
} else {
this.el.nativeElement.value = 0;
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Angularjs 動(dòng)態(tài)改變title標(biāo)題(兼容ios)
這篇文章主要介紹了 Angularjs 動(dòng)態(tài)改變title標(biāo)題(兼容ios)的相關(guān)資料,需要的朋友可以參考下2016-12-12
在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法
今天小編就為大家分享一篇在angularJs中進(jìn)行數(shù)據(jù)遍歷的2種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
AngularJS入門教程之與服務(wù)器(Ajax)交互操作示例【附完整demo源碼下載】
這篇文章主要介紹了AngularJS與服務(wù)器Ajax交互操作的方法,可實(shí)現(xiàn)post傳輸數(shù)據(jù)的功能,并附帶完整的demo源碼供讀者下載參考,源碼中還包含了前面章節(jié)的示例文件,需要的朋友可以參考下2016-11-11
Angular.js中$apply()和$digest()的深入理解
相信大家都知道$digest()和$apply()是AngularJS中的兩個(gè)核心并且有時(shí)候容易引人誤解的部分。我們需要深入理解這兩者是如何運(yùn)作的,從而才能理解AngularJS本身是如何運(yùn)作的。本文的目的就是介紹$digest()和$apply()是如何確確實(shí)實(shí)的對(duì)你有用的。下面來(lái)一起看看吧。2016-10-10
在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的步驟詳解
本文分步驟給大家介紹了在 Angular6 中使用 HTTP 請(qǐng)求服務(wù)端數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
AngularJS實(shí)現(xiàn)路由實(shí)例
本文給大家分享的是使用angularjs路由框架實(shí)現(xiàn)的一個(gè)簡(jiǎn)單頁(yè)面跳轉(zhuǎn)功能,非常的實(shí)用,也很詳細(xì),有需要的小伙伴可以參考下2017-02-02
Angularjs通過(guò)指令監(jiān)聽ng-repeat渲染完成后執(zhí)行腳本的方法
指令是angular的核心功能之一,用好了事半功倍,監(jiān)聽ng-repeat執(zhí)行狀態(tài)僅僅是它功能的冰山一角吧。下面這篇文章主要介紹了Angularjs通過(guò)指令監(jiān)聽ng-repeat渲染完成后執(zhí)行腳本的方法,需要的朋友可以參考下。2016-12-12
Angular.JS學(xué)習(xí)之依賴注入$injector詳析
隨著javaEE的spring框架的興起,依賴注入(IoC)的概念徹底深入人心,它徹底改變了我們的編碼模式和思維。在AngularJS中也有依賴注入的概念,像spring中的依賴注入,但是又有所不同。Angular中只需要在需要的地方聲明一下即可,類似模塊的引用,因此十分方便。2016-10-10
AngularJS中的Promise詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了AngularJS中的Promise詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,Promise是一種模式,以同步操作的流程形式來(lái)操作異步事件,避免了層層嵌套,可以鏈?zhǔn)讲僮鳟惒绞录?,需要的朋友可以參考?/div> 2016-12-12最新評(píng)論

