Skip to content

createComponent function example not work on 16.2.1 #51492

Closed

Description

@hiepxanh

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

Yes

Description

The document wrong: https://angular.io/api/core/createComponent

const componentRef = [createComponent](https://angular.io/api/core/createComponent)(HelloComponent, {host, environmentInjector});

host was change to hostElement, should become this
const componentRef = [createComponent](https://angular.io/api/core/createComponent)(HelloComponent, {hostElement, environmentInjector});

Btw it just not work.

Please provide a link to a minimal reproduction of the bug

[this one work]
https://stackblitz.com/edit/angular-anxyrb?file=src%2Fmain.ts,src%2Findex.html
[not work version]
https://codesandbox.io/p/sandbox/relaxed-feather-27fv8y?file=/src/app/app.component.ts:63,6

Please provide the exception or error you saw

NG0912: Component ID generation collision detected. Components 'HelloComponent' and 'HelloComponent' with selector 'hello-component' generated the same component ID. To fix this, you can change the selector of one of those components or add an extra host attribute to force a different ID. Find more at https://angular.io/errors/NG0912

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

ng version result:

image

Activity

  1. changed the title [-]createComponent function example not work[/-] [+]createComponent function example not work on 16.2.1[/+] on Aug 24, 2023
  2. added this to the needsTriage milestone on Aug 24, 2023
  3. added
    good first issueLabel noting a good first issue to be worked on by a community member
    on Aug 24, 2023
  4. JeanMeche commented on Aug 24, 2023

    @JeanMeche
    Member

    FYI it's missing a CD cycle.
    Calling componentRef.changeDetectorRef.detectChanges(); fixes your issue.

  5. hiepxanh commented on Aug 24, 2023

    @hiepxanh
    ContributorAuthor

    @JeanMeche I just try to copy the document to make sure it follow tutorial. it's still not work sir, I updated as your suggest but it still the same

    image

    image

  6. JeanMeche commented on Aug 24, 2023

    @JeanMeche
    Member

    You're missing import 'zone.js'. (bootstrapApplication() throws silently an NG908)

  7. hiepxanh commented on Aug 24, 2023

    @hiepxanh
    ContributorAuthor

    aw great, finally it start running. Thank you sir @JeanMeche

  8. hiepxanh commented on Aug 24, 2023

    @hiepxanh
    ContributorAuthor

    I created PR to update this

  9. hiepxanh commented on Aug 24, 2023

    @hiepxanh
    ContributorAuthor

    anyway, lazy is so bad, that repo is not complex enough. I think the current situation should look like this example repo:
    https://codesandbox.io/p/sandbox/relaxed-feather-27fv8y?file=/src/app/app.component.ts:63,6
    @JeanMeche

    refer from this issue: #51491

  10. hiepxanh commented on Aug 24, 2023

    @hiepxanh
    ContributorAuthor

    the dynamic 1 and dynamic 2 should jump to here, and I already use change detection to update it:

    image

  11. hiepxanh commented on Aug 27, 2023

    @hiepxanh
    ContributorAuthor

    @hiepxanh thanks for creating a repro. The problem is that you've used a binding value that would always be different for each change detection cycle, i.e. <div [innerHTML]="domSanitizer.bypassSecurityTrustHtml(content)"></div>. The bypassSecurityTrustHtml would return a new object each time it's invoked. You can try adding a new property to a class that would store a reference to the function call result and use it in a template instead. For example:

    @Component({
      standalone: true,
      selector: 'my-app',
      template: `<div [innerHTML]="sanitizedContent"></div>`,
    })
    export class AppComponent {
      domSanitizer = inject(DomSanitizer);
    
      content = `<h1> hello world 1</h1>  
        <dynamic1> </dynamic1> 
        <dynamic2> </dynamic2>  
        <h1> hello world 2</h1>`;
    
      // Store a reference to the computed result and use it in a template
      sanitizedContent = this.domSanitizer.bypassSecurityTrustHtml(this.content);
    

    Thank you.

    copy from #51491 (comment)

    dynamic1 can render successful with variable reference instead of function because it's new object everytime
    solve the issue I'm facing now.

    I will add an example about this in the document

  12. hiepxanh commented on Aug 28, 2023

    @hiepxanh
    ContributorAuthor

    Hello sir, hope you are doing well. @AndrewKushnir I think this may be my last issue about this problem. I can have a perfect reproduction on our issue. I created on a new issue here: #51535

  13. angular-automatic-lock-bot commented on Oct 3, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  14. locked and limited conversation to collaborators on Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueLabel noting a good first issue to be worked on by a community memberstate: has PR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions