1

In an ionic app, i am using a angular component. In that angular component, i have a variable headerText which in initialized from the page where this component is used.

Problem is that headerText variable is always undefined.

How can i fix this problem?

This is the angular component where headerText variable is defined.

import { Component, Input, OnInit } from '@angular/core';
import { MenuController, NavController } from 'ionic-angular';

@Component({
  selector: 'custom-header-text',
  templateUrl: 'custom-header-text.html'
})
export class CustomHeaderTextComponent implements OnInit {

  @Input() headerText: string;

  constructor(private navCtrl :  NavController,
              private menuCtrl : MenuController) {

  }

  ngOnInit() {
    setTimeout(() => {
      console.log('text = ' + this.headerText);
    }, 3000);
  }

  goBack() {
    this.navCtrl.pop();
  }

  openMenuPage() {
    this.menuCtrl.enable(true,'bl-menu')
    this.menuCtrl.open();
  }
}

This is how i am passing a value to this headerText variable from where this component is used.

<custom-header-text [headerText]="'Inbox'"></custom-header-text>
8
  • Are you getting any errors on the console? Commented Oct 15, 2018 at 16:33
  • no, there are no errors Commented Oct 15, 2018 at 16:34
  • Can you replicate this issue with a Sample StackBlitz? Commented Oct 15, 2018 at 16:35
  • Are you calling the custom-header-text selector in index.html or your root element? Commented Oct 15, 2018 at 16:52
  • @Marshal i am using custom-header-text selector in an ionic page. Commented Oct 15, 2018 at 16:54

2 Answers 2

1

See below.

The reason why this is not working is that your root element in which you place the <custom-header-text [headerText]="'Inbox'"></custom-header-text> is not an angular component. Because of this, Angular won't compile this element. And Angular does not read attribute values during runtime, only during compile time, as otherwise we would get a performance hit.

Answered here

Angular 2 input parameters on root directive

Sign up to request clarification or add additional context in comments.

2 Comments

So that means i cannot pass data to the angular component in this way from the root element in ionic?
There is an example in the linked answer that shows how to get string values, you could possibly use that here as you want a string let prop = eltRef.getAttribute('headerText'); but anything beyond a string value I believe you will not be able to. I ran into this in my project doing a browser check, I will keep looking to see if there is a way for data beyond a string but getAttribute should work for your string.
0

Your code wasn't working as the HeaderModule was not correctly configured.

There were a few issues with your implementation:

  1. The path to the Header Module wasn't correctly set in AppModule.
  2. The path to the HeaderComponent template was also not set to the correct HTML file.

After fixing those, it was working as expected. Here's an Updated StackBlitz for your ref.

1 Comment

yes, i couldn't set the correct paths to files on Stackblitz but in my project on my pc, paths are correctly configured, setting another page as root page fixed this issue.

Your Answer

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