1

I have angular2 final version. Have created a component

  @Component({
    selector:'artists',
    template: `
      <h2>Artists Information</h2>
      <ul [style.display]="artists?.length>0 ? 'block':'none'">
        <li ngfor="a in artists">Name: {{a?.name}} - Followers:  {{a?.folloers.total}}</li>
      </ul>
       `
  })
  export class Artists{
    @Input("list")
    private artists:any;
  }

at a later time somewhere in the program i try to dynamically load the component

  @ViewChild('divBody', {read: ViewContainerRef}) private body: ViewContainerRef;

  this.body.clear();
  let factory = this.resolver.resolveComponentFactory(Artists);
  let injector = ReflectiveInjector.fromResolvedProviders([], this.body.parentInjector);   
  let b= this.body.createComponent(factory,0,injector,[]);

  // data is comming from an ajax call to a remote location.
  b.instance.artists = data.artists.items;
  b.changeDetectorRef.detectChanges();

My problem is that the Artists view is not updated and shows a blank list row instead of list of artists.

Question is what am i missing? why template is not update with data even after i call detectChanges() ????

0

1 Answer 1

2
<li ngfor="a in artists"

should be

<li *ngFor="let a of artists"

If you have no null elements in artists you don't need ? in {{a?.name}} (within *ngFor)

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

3 Comments

Thank you very much. that little change fixed the problem.
@Masoud if the answer solves your problem, please consider accepting the answer using the checkmark below the up/down-vote button to indicate your problem is solved.
I've added bunch of samples on plunker.. hope they become used by the public... "plnkr.co/users/msalehisedeh"

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.