项目作者: piyalidas10

项目描述 :
Angular Validation Tooltip Directive
高级语言: TypeScript
项目地址: git://github.com/piyalidas10/validation-tooltip-directive.git


Validation-Tooltip-Directive-in-Angular-6

Live URL

https://piyalidas10.github.io/validation-tooltip-directive/

RUN Application

  1. ng serve
  2. http://localhost:4200

Mockdata

fieldInfoContent.json in assets folder

  1. {
  2. "fieldInfo": {
  3. "firstName" : "Firstname is a required field",
  4. "middleName": "middleName is not a required field",
  5. "lastName" : "Lastname is a required field",
  6. "emailId": "Email is a required field",
  7. "mobile" : "Mobile is a required field",
  8. "password" : "Password is a required field"
  9. }
  10. }

validation-msg.service.ts

  1. import { Injectable } from '@angular/core';
  2. @Injectable({
  3. providedIn: 'root'
  4. })
  5. export class ValidationMessageService {
  6. validationErrorObj = [];
  7. public getValidationMsg(validationId: string): string {
  8. return this.validationErrorObj[validationId];
  9. }
  10. }

Load all messages from fieldInfoContent.json using the following code which is written in register.component.ts

  1. validationErrorMsg() {
  2. this.apiService.getFieldInfoMessage().then(
  3. (res) => {
  4. if (this.validErrorMsgService.validationErrorObj.length === 0) {
  5. this.fieldInfoMsgArr = res['fieldInfo'];
  6. console.log('Field Info Array => ', this.fieldInfoMsgArr);
  7. this.isLoading = false;
  8. }
  9. }, (error) => {
  10. console.log(error);
  11. this.isLoading = false;
  12. });
  13. }

Now i have to create a directive validation-label.directive.ts will run on the status change of the form control elements. It requires a ‘formControlName’ attribute added to the control. This will be used to used to construct the key that will be passed to the validation service class. The above directive will handle the change/blur events on controls and displays the messages accordingly.