Angular 4: Calling regex patterns from an object

I have a problem with the code, but I am getting a Tslint error that I cannot understand. This setup worked when using Angular 1, but now I am converting my app to 4 using angular-cli.

Angular: The id 'vin' is undefined. The object does not contain such members.

I have generic Regex templates stored in the object shown below. It is imported into my component and then called with [pattern]="patterns.KEY"

.

The template works just as it should and the validation works, but I still get the above error when I attach it to any of my inputs. Is it because I am missing data types on patterns

? If so, how will this be determined?

The error is recorded only in the template file. The component and templates file has no validation errors.

Template code:

<div class="form-group">
    <label for="vin">VIN</label>
    <input type="text" class="form-control" id="vin" placeholder="VIN"
           minlength="8" maxlength="17" required [pattern]="patterns.vin"
           [(ngModel)]="model.Vin" name="Vin" #Vin="ngModel">
  </div>

      

Component code:

export class COMPONENT_NAME implements OnInit {
  patterns = Patterns;
}

      

Template file:

export const Patterns: Object = {
  vin: /^[\w\d]+$/i,
};

      

+3


source to share


1 answer


It turns out the answer was pretty simple. patterns

needs to be defined, but the value to be set internally ngOnInit()

:

export class COMPONENT_NAME implements OnInit {
  patterns;
  ngOnInit() {
    this.patterns = Patterns;
  }
}

      



This fixes the Tslint issue I saw.

+1


source







All Articles