0

I am attempting to streamline my application by using a single template to reference a custom element, and then specify the contents via the @Input() property decorator. The method I am using isn't working and I'm not sure why, my code is as follows:

Component.ts

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'side-menu-option',
  templateUrl: './side-menu-option.component.html',
  styleUrls: ['./side-menu-option.component.css']
})
export class SideMenuOptionComponent implements OnInit {

  @Input() icon: string; // Inputs
  @Input() text: string;

  constructor() { }

  ngOnInit() {
  }

}

Component.html

<button class="buttons button-style btn-block" type="button"><i class="mdi mdi-{{icon}}"></i><span class="menu-text">{{text}}</span></button>

OtherComponent.html

<div class="side-nav text-center">
  <div class="side-nav-container">
    <img class="avatar" src="http://placehold.it/150x150">
    <side-menu-option [icon]="home" [text]="Home"></side-menu-option>
    <side-menu-option [icon]="file-document" [text]="Documents"></side-menu-option>
  </div>
</div>

[icon] and [text] are not being recognized for some reason and I am not sure why. It worked before I tried to refactor it, but for some reason these inputs are not being loaded.

Thanks

4 Answers 4

1

Well you have to understand that [icon]="statement here" takes a statement but you are passing it a string so you have to wrap your string in quotes again to make it a string explicitly.

<div class="side-nav text-center">
  <div class="side-nav-container">
    <img class="avatar" src="http://placehold.it/150x150">
    <side-menu-option [icon]="'home'" [text]="'Home'"></side-menu-option>
    <side-menu-option [icon]="'file-document'" [text]="'Documents'"></side-menu-option>
  </div>
</div>

Note: This is also a good practice because one can look at it and will know that these are input properties but if you remove these brackets[] they can be easily confused for attributes. Just want to mention, there is no harm in either approach.

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

Comments

0

Bind without brackets if your inputs aren't any parent attributes

<div class="side-nav text-center">
  <div class="side-nav-container">
    <img class="avatar" src="http://placehold.it/150x150">
    <side-menu-option icon="home" text="Home"></side-menu-option>
    <side-menu-option icon="file-document" text="Documents"></side-menu-option>
  </div>
</div>

Comments

0
<div class="side-nav text-center">
  <div class="side-nav-container">
    <img class="avatar" src="http://placehold.it/150x150">
    <side-menu-option icon="home" text="Home"></side-menu-option>
    <side-menu-option icon="file-document" text="Documents"></side-menu-option>
  </div>
</div>

Remove the property binding syntax ([]brackets) for the Input properties in parent component ([item] => item)

Comments

0

EDIT: I first misunderstood the question, the problem actually is that you should use 'value' for your values, because Angular will look for variables called home, file-document, and so on. You should use this instead:

<side-menu-option [icon]="'home'" [text]="'Home'"></side-menu-option>
<side-menu-option [icon]="'file-document'" [text]="'Documents'"></side-menu-option>

2 Comments

Is this a better method as opposed to simply removing the brackets?
@JarredParr I think Raheel Anwar did answer your question in his answer.

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.