Angular4 calling a function in constructor

こ雲淡風輕ζ 提交于 2021-01-27 11:20:57

问题


I'm trying to call a modal function in the constructor in Angular 4 but the function get highlighted that is not properly called and when the page is loaded not error is read in the log and the modal is not popping up as its suppose to. The screen gets dark alright but the text in the modal doesn't show up.

constructor(public formBuilder: FormBuilder,
            public router: Router,
            public toastr: ToastrService,
            public http: HttpClient,
            public modalService: BsModalService,) {
  if (this.getWardData) {
    this.displayHint();
  }
}

displayHint(template: TemplateRef<any>) {
  this.modalRef = this.modalService.show(template, {class: 'modal-sm'});
}

HTML

<ng-template #template>
  <div class="modal-body text-center">
    <p>Do you want to Continue where you left?</p>
    <button type="button" class="btn btn-default" (click)="confirm()" >Yes</button>
    <button type="button" class="btn btn-primary" (click)="decline()" >No</button>
  </div>
</ng-template>

回答1:


Try to use the following code

constructor(public formBuilder: FormBuilder,
    public router: Router,
    public toastr: ToastrService,
    public http: HttpClient,
    public modalService: BsModalService, ) {
    // if (this.getWardData) {
    //   this.displayHint();
    // }
  }

  ngOnInit() {
    if (this.getWardData) {
      this.displayHint();
    }
  }

  displayHint(template: TemplateRef<any>) {
    this.modalRef = this.modalService.show(template, { class: 'modal-sm' });
  }



回答2:


I think you have problem with modal template. You can run your modal but you need to pass to displayHint method template parameter(TemplateRef). In you view I see you have this template but you don't have reference to this template in component implementation. Add this part of code to your component(reference to your modal template - you need this to show modal):

@ViewChild('template') private modalTemplate: TemplateRef<any>;

Remove this.displayHint() from your constructor(I explained in below), add ngAfterViewInit on ngOnInit implementation and there add displayHint method call:

export class YourComponentName implements AfterViewInit {
    @ViewChild('template') private modalTemplate: TemplateRef<any>;

    getWardData = true; // for example purposes - always true

    constructor(public formBuilder: FormBuilder,
                public router: Router,
                public toastr: ToastrService,
                public http: HttpClient,
                public modalService: BsModalService
    ) {}

    ngAfterViewInit() {
        if (this.getWardData) {
            this.displayHint(this.modalTemplate);
        }
    }

    displayHint(template: TemplateRef<any>) {
      this.modalRef = this.modalService.show(template, {class: 'modal-sm'});
    }
}

There’s a huge difference between constructor and ngOnInit/ngAfterViewInit of the component. Angular bootstrap process consists of the two major stages:

  • constructing components tree
  • running change detection

Your controller method is running in "Constructing components tree" stage

(reference to modal template is undefined here)

Your ngOnInit method is running in "Running change detection" stage.

(reference to modal template is defined here)

The @Input communication mechanism is processed as part of following change detection phase so input bindings are not available in constructor.

So you can't run your modal from constructor

More about lifecycle hooks you can find here

Live working example you can find here



来源:https://stackoverflow.com/questions/54770815/angular4-calling-a-function-in-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!