2

Is there any way to pas parameters to the root of an Angular 2 Component? I have been able to do this with the components that are part of the internal component template but not with the root component:

<my-component [test] = "some-val"></my-component>

On my component:

export class AppComponent implements OnInit {
    test: string;

    ngOnInit() {
        console.log(this.test); //test is undefined
    }
}

The reason to do this is use Razor variables to be pass to Angular. Like editing a user for instance. I could do something like:

<edit-form [model] = "@Model.Id" > </edit-form>
Mario Lopez
  • 1,405
  • 13
  • 24

1 Answers1

2

Angular does not do any binding from index.html only from within other components.

A workaround to get attribute values in the root component is

export class AppComponent {
  constructor(elementRef:ElementRef) {
    console.log(elementRef.nativeElement.getAttribute('inputField));
  }
}

See also https://github.com/angular/angular/issues/1858

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567