I'm aware this is not the best solution but I would like to be able to load components dynamically from a JSON response, something along these lines:
app.component
@Component({
selector: 'my-app',
template: '<h1>My First Angular 2 App</h1> {{component.title}} {{component.selector}}',
providers: [AppService],
directives: [ExampleComponent]
})
export class AppComponent implements OnInit {
component:{};
constructor(
private _appService: AppService) {
}
ngOnInit() {
this.component = this._appService.getComponent();
}
}
app.service
@Injectable()
export class AppService {
component = {
title: 'Example component',
selector: '<example></example>'
}
getComponent() {
return this.component;
}
}
example.component
@Component({
selector: 'example',
template: 'This a example component'
})
export class ExampleComponent {
}
If I run this example, my output is <example></example>
but it doesn't actually render the component. Also I've tried to use [innerHtml]="component.selector"
, but that also didn't work. Does anyone have an idea or suggestion?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…