we're trying to build a nice Angular 2 app here.
We want to load a few components based on what we get back from a web service call.
You get json back from the web service call in our base component.
The json may look like this:
[{
"textChunk": {
"text": "Lorem ipsum dolor sit amet"
}
}, {
"factsBox": {
"heading": "Sample heading",
"body": "<p>Lorem ipsum sample</p><p>More sample rows</p>"
}
}, {
"chart": {
"type": "circle",
"data": [30, 10, 50, 10]
}
}]
So, we get "textChunk", "factsBox", "chart" back and their respective data.
Now in our base component's ngOnInit method, we want to somehow load/inject:
- textChunk component
- factsBox component
- chart component
with their respective templates (preferably html files or ASP.NET MVC area views) and give them a model (stored in an object model in our base component).
Also, these items in the json array might appear multiple times at different positions - that's why we need to load them in the correct component on the fly.
I've been looking into deprecated DynamicContentLoader, I've been looking at this thorough SO answer on dynamically loading components and I've been looking at some other stuff. I try to understand the SO answer, but fail to do so.
Can you point me in the right direction with base examples on how I can dynamically load my components and their templates?
To give you slightly more insight, here's the our base component:
import { Component, Directive, Input, OnInit } from '@angular/core'
import { ApiService } from './api.service';
import { IApiItem } from './IApiItem';
import { KeysPipe } from './KeysPipe';
@Component({
selector: 'sampleApi',
templateUrl: 'app/templates/latest.html',
providers: [ApiService]
})
export class ApiComponent implements OnInit {
constructor(private apiService: ApiService) { }
model: IApiItem;
ngOnInit() {
this.get();
}
get() {
this.apiService.getJson().subscribe(data => this.model = data);
}
}
Thanks!