I am trying to create a mask directive in Angular2
by injecting NgModel
. However, when the directive is placed in a tag with ngControl
, an exception is thrown:
angular2.dev.js:23877 EXCEPTION: No provider for NgModel! (MaskDirective -> NgModel)
HTML:
<input type="text" class="form-control" id="telephone" name="telephone" placeholder="123-456-7890" [(ngModel)]="contactInfo.telephone" required ngControl="telephone" #telephone="ngForm" mask-input="(***) ***-****" [modify]="contactInfo.telephone"/>
Component constructor:
constructor(public model: NgModel, public ele: ElementRef, @Attribute("mask-input") maskPattern: string) {
this.dividers = maskPattern.replace(/\*/g, "").split("");
this.dividers.push("_");
this.generatePattern(maskPattern);
this.numOfChar = 0;
}
this is the plunker: https://plnkr.co/edit/TlVWlQL2FpDeuoAMiy6z?p=preview
Injecting NgControl doesn't give access to the model value. What is the recommended solution for this?
Thanks in advance!