0

I am trying to bind the input with the component as defined here :

https://angular.io/docs/ts/latest/cookbook/component-communication.html#!#parent-to-child but component name is never shown

I am even trying to do the console.log but it shows:

componentName isundefined

<div ng-switch="accessLevel">
<div class="customer" ng-switch-when="ENABLED">This is customer data</div>
<div class="customer-blurr"  ng-switch-when="DISABLED"> This is disabled Customer Data</div>
<div class="customer-blurr" ng-switch-default> <demo-error [componentName]="componentname"></demo-error></div>
</div>

and in error.html

<div> you are not allowed to access {{component}} </div>

Demo Error :

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

@Component({
  selector: 'demo-error',
  templateUrl: './error.component.html',
  styleUrls: ['./error.component.css']
})
export class ErrorComponentDemo implements OnInit {
@Input() public componentName:string;
  constructor() { 
    console.log("componentName is" +this.componentName)
  }

  ngOnInit() {
  }

}

And in **CustomerComponent:**

@Component({
  selector: 'customer',
  templateUrl: './customer.component.html',
  styleUrls: ['./customer.component.css']
})
export class CustomerComponent extends SuperChildComponent{
  public allowed: boolean = false;
  public  accessLevel:AccessLevel =null;
  public componentname:string;

  constructor(private authenticationService : AuthorizationService) {
    super();
    this.componentname=this.constructor.name;
     this.accessLevel=this.authenticationService.isUserLoggedIn()?this.authenticationService.componentAccessLevel(this.constructor.name):null;
  }

what am i missing here ?

Thanks

2 Answers 2

2

Your input won't be available in the constructor yet. Try this f.e.

@Input set componentName(value: string) { 
  if(value != null && value != undefined) {
    this._componentName = value; console.log(this._componentName); 
  }
}

_componentName: string

This way you can do your calls of methods in the if statement within the set

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

Comments

2

You need to lose the '[]' if your input is a string.

componentName="componentname"

Comments

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.