0

I am trying to get the ngModel value when it is un-touched, pristine on OnInit, all I am trying to achieve is when I do a request.GET and bind that value to ngModel to catch it in the directive while is un-touched.

for example:

<label for="employeeShare">Arbeitnehmer Anteil</label>&nbsp;<span class="requirered">*</span>
                            <input type="number" class="form-control" id="employeeShare" required placeholder="5.125%"
                                ***toPercentage***
                                name="employeeShare"
                                [ngModel]="data.insObj.stat['AHV,IV,EO']?.employeeShare"
                                (ngModelChange)="data.insObj.stat['AHV,IV,EO'] && data.insObj.stat['AHV,IV,EO'].employeeShare = $event">
                                <i class="form-control-feedback">%</i>

this is my Input field, and my directive is called toPercentage.

I am trying to get the value of this ngModel in the directive while is still un-touched, pristine onInit or whatever lyfecicle I could get this value. how can I achieve this

for example:

ngOnInit() {
    console.log(this.el.nativeElement.value);
    this.el.nativeElement.value * 100

}

onInputChange(event) {
    console.log(this.el.nativeElement.value);
    this.el.nativeElement.value / 100
}
  • What does the GET request look like? Why don't you get it from `"data.insObj.stat['AHV,IV,EO']?.employeeShare"`? – Günter Zöchbauer Apr 21 '17 at 08:30
  • What's the purpose of accessing it using `nativeElement`? – Günter Zöchbauer Apr 21 '17 at 08:30
  • @GünterZöchbauer, I am trying to get the NgModel value in the directive while onInit –  Apr 21 '17 at 08:32
  • What for? Why using `nativeElement`? What directive anyway? There is no guarantee `ngModel` already assigned a value when `ngOnInit` of the directive is called. If you fetch the data using a server request, I'd rather guarantee that the won't be a value yet at this time. – Günter Zöchbauer Apr 21 '17 at 08:34
  • I am trying to create a directive for converting numbers to percenteage and vice versa, so if it has value I will multiple it by 100 and when I send to the serve onNgModelChange() I will divide it by 100 –  Apr 21 '17 at 08:36
  • There should be several examples with different approaches out there for such use cases. As I said, if there is no value, it's unlikely to get it, no matter what approach you're using. – Günter Zöchbauer Apr 21 '17 at 08:38
  • @GünterZöchbauer, I updated my question, and please If you have any resource where I can find some informations about my approach can you share it . Thanks for the support –  Apr 21 '17 at 08:41
  • @GünterZöchbauer, can you just show me a way, how to get the value if for example it has a value and onInit to catch that, I have achieved this with lyfecicle of ngAfterContentChecked() but it get's fired too much times, I need it only once –  Apr 21 '17 at 09:19
  • http://stackoverflow.com/questions/37800841/input-mask-fields-in-angular2-forms/37887432#37887432 might help, but there might be better approaches. I don't have deep knowledge here either. – Günter Zöchbauer Apr 21 '17 at 09:29

0 Answers0