0

I'm very new to web-development in general, but I'm trying to build my website and came across the following problem I haven't be able to solve. I'm using angular 4 I have a div highlighting some code in my html file:

<div *ngIf="step1Updated">
  <prism-block 
    [code]="step1" 
    [language]="'python'">
  </prism-block>
  {{ step1 }}
</div>

In my typescript file:

step1Updated = false;
step1 = '';

onStep1(fileType: string) {
    this.step1 = this.step1service.returnCode(this.language, fileType);
    this.step1Updated = true;
}

Now on my html page, when I click from a selection of buttons executing onStep1() with different content, the content within my string interpolation changes, the {{ step1 }}, but the content within my prism-block doesn't. I can see that this is because we need two-way databinding but I've tried to put the [(ngModel)]="step1" two way data binding in the prism-block, div, etc... hoping that it would catch the update and then update the block, same as putting code in [(code)] but all resulted in errors..

any help or advice would be really appreciated!

13
  • you can't use ngModel outside of forms, show your prism-block component definition Commented Jun 25, 2017 at 5:57
  • I did not create the prism-block component, it was installed via npmjs.com/package/angular-prism, with the imports and declaration being in the app.module.ts. Commented Jun 25, 2017 at 6:01
  • there's something wrong with prism component, can you put up a plunker? Commented Jun 25, 2017 at 6:04
  • I've never used plunker, I just tried to but it works nothing like the angular I've been using.. My prism works, it highlights code from multiple languages fine, just if change the content of a string, it wont show the updated content. Commented Jun 25, 2017 at 6:16
  • drop here the link to the plunker you created Commented Jun 25, 2017 at 6:30

1 Answer 1

1
here you need to load PrismComponent dynamically to update its view.refer my plunker(http://plnkr.co/edit/tEgnnS) code.


    onStep1(fileType: string) {
    const crf = this.cfR.resolveComponentFactory(PrismComponent);
    this.cc.clear();
    const cf = this.cc.createComponent(crf);
    (<PrismComponent>cf.instance).code = this.step1service.returnCode(this.language, fileType);
  }
Sign up to request clarification or add additional context in comments.

2 Comments

refer app template and its component, don't forget to add entryComponent in app module
Worked perfectly, repeating the last line to load the language dynamically too. Now I will study your code to fully understand it. Thank you so much!

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.