3

I got the below error on Ionic 3 app.

TypeError: Cannot read property 'get' of undefined
    at Object.eval [as updateDirectives] (ng:///NewLoginPageModule/NewLoginPage.ngfactory.js:196:39)
    at Object.debugUpdateDirectives [as updateDirectives] (http://localhost:8100/build/vendor.js:14693:21)
    at checkAndUpdateView (http://localhost:8100/build/vendor.js:13862:14)
    at callViewAction (http://localhost:8100/build/vendor.js:14212:21)
    at execComponentViewsAction (http://localhost:8100/build/vendor.js:14144:13)
    at checkAndUpdateView (http://localhost:8100/build/vendor.js:13868:5)
    at callViewAction (http://localhost:8100/build/vendor.js:14212:21)
    at execEmbeddedViewsAction (http://localhost:8100/build/vendor.js:14170:17)
    at checkAndUpdateView (http://localhost:8100/build/vendor.js:13863:5)
    at callViewAction (http://localhost:8100/build/vendor.js:14212:21)

.ts

    ulLoginForm: FormGroup;

    constructor(private formBuilder: FormBuilder) {}

     ionViewDidLoad() {
       this.initForm();
     }

  initForm() {
    this.ulLoginForm = this.formBuilder.group({
       password: ['', Validators.required]
    })
  }

.html

 <form [formGroup]="ulLoginForm">                           
        <ion-list>
         <ion-item>
        <ion-input type="password" placeholder="Password"  formControlName="password"></ion-input>
        <p *ngIf="ulLoginForm.get('password').hasError('required') && ulLoginForm.get('password').touched" class="error" padding-left>Password
        is empty</p>
         </ion-item>
        </ion-list>
    </form>
8
  • call this.initForm(); in constructor. Commented Aug 21, 2018 at 11:27
  • 1
    It is not a good coding practice according to the Angular team. e.g. When we do a Unit testing and etc @fatemefazli Commented Aug 21, 2018 at 11:28
  • @Sampath can you explain this? Commented Aug 21, 2018 at 11:31
  • btw @Sampath where did you call ionViewDidLoad()?? Commented Aug 21, 2018 at 11:33
  • It is a life cycle hook of Ionic 3: @SaurabhAgrawal ionicframework.com/docs/api/navigation/NavController Commented Aug 21, 2018 at 11:35

3 Answers 3

3

the problem is that you want to access the formControl, but the initialization of the form is after the initialization of the component (because the ionViewDidLoad is executed after).

you could try to call the initForm() in the ngOnInit method, this is a lifecycle method of the angular framework:

export class YourComponent implements OnInit{    
    ulLoginForm: FormGroup;

    constructor(private formBuilder: FormBuilder) {}

    ngOnInit() {
       this.initForm();
     }
}
Sign up to request clarification or add additional context in comments.

Comments

1

If you can use angular life cycle hooks:

DEMO

export class HomePage implements OnInit {

  ngOnInit() {
    this.initForm();
  }
  ulLoginForm: FormGroup;

  constructor(private formBuilder: FormBuilder) { }
  initForm() {
    this.ulLoginForm = this.formBuilder.group({
      password: ['', Validators.required]
    })
  }
}

Comments

1

According to the Ionic 3 doc, I was right about my implementation above.

ionViewDidLoad void

Runs when the page has loaded. This event only happens once per page being created. If a page leaves but is cached, then this event will not fire again on a subsequent viewing. The ionViewDidLoad event is good place to put your setup code for the page.

But it is wrong when I implemented it. We need to use Angular lifecycle hook here like so:

export class NewLoginPage implements OnInit {

  ngOnInit(){
     this.initForm();
   }
}

2 Comments

I wanted to give more info to the Ionic 3 community about the issue of ionViewDidLoad() method. That is why I have posted it here. @UnluckyAj
fine from my side. but you can also edit my answer if you want to put more content

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.