Is it possible to select by id in Angulars ?










1















I'm playing around with Angular's (version 7) content projection. As far as I know, it's possible to select by attribute, class, and tag with the select attribute of <ng-content>.



I've also tried to select by id:



<ng-content select="#myID"></ng-content>


From:



<mycomponent>
<div id="myid">
Test
</div>
</mycomponent>


But it doesn't seem to work.



Why does the selection of IDs not work?










share|improve this question
























  • for me is very exotic to use <div id=...> in angular template - can you explain why you wanna use it?

    – Kamil Kiełczewski
    Nov 13 '18 at 16:58











  • I got asked this by someone. Why is it uncommon in Angular to do this? I expected that I can use some kind of CSS selector within the select.

    – Robin
    Nov 13 '18 at 17:00






  • 1





    usually you should not use any selector to get html element in your typescript code, but OPPOSITE - usually inside your html template you use your typescript objects by using directives like *ngIf, *ngFor,, (click)="someFunction(xx)" [(ngModule)]...

    – Kamil Kiełczewski
    Nov 13 '18 at 17:19







  • 1





    so usually trying to get some html-nodes inside .ts file is wrong approach to angular... The right approach is to use javascript (typescript) object inside template (not template elements inside javascript)

    – Kamil Kiełczewski
    Nov 13 '18 at 17:20
















1















I'm playing around with Angular's (version 7) content projection. As far as I know, it's possible to select by attribute, class, and tag with the select attribute of <ng-content>.



I've also tried to select by id:



<ng-content select="#myID"></ng-content>


From:



<mycomponent>
<div id="myid">
Test
</div>
</mycomponent>


But it doesn't seem to work.



Why does the selection of IDs not work?










share|improve this question
























  • for me is very exotic to use <div id=...> in angular template - can you explain why you wanna use it?

    – Kamil Kiełczewski
    Nov 13 '18 at 16:58











  • I got asked this by someone. Why is it uncommon in Angular to do this? I expected that I can use some kind of CSS selector within the select.

    – Robin
    Nov 13 '18 at 17:00






  • 1





    usually you should not use any selector to get html element in your typescript code, but OPPOSITE - usually inside your html template you use your typescript objects by using directives like *ngIf, *ngFor,, (click)="someFunction(xx)" [(ngModule)]...

    – Kamil Kiełczewski
    Nov 13 '18 at 17:19







  • 1





    so usually trying to get some html-nodes inside .ts file is wrong approach to angular... The right approach is to use javascript (typescript) object inside template (not template elements inside javascript)

    – Kamil Kiełczewski
    Nov 13 '18 at 17:20














1












1








1


1






I'm playing around with Angular's (version 7) content projection. As far as I know, it's possible to select by attribute, class, and tag with the select attribute of <ng-content>.



I've also tried to select by id:



<ng-content select="#myID"></ng-content>


From:



<mycomponent>
<div id="myid">
Test
</div>
</mycomponent>


But it doesn't seem to work.



Why does the selection of IDs not work?










share|improve this question
















I'm playing around with Angular's (version 7) content projection. As far as I know, it's possible to select by attribute, class, and tag with the select attribute of <ng-content>.



I've also tried to select by id:



<ng-content select="#myID"></ng-content>


From:



<mycomponent>
<div id="myid">
Test
</div>
</mycomponent>


But it doesn't seem to work.



Why does the selection of IDs not work?







angular angular7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:48









Daniel W Strimpel

3,3961617




3,3961617










asked Nov 13 '18 at 16:53









RobinRobin

3,77423259




3,77423259












  • for me is very exotic to use <div id=...> in angular template - can you explain why you wanna use it?

    – Kamil Kiełczewski
    Nov 13 '18 at 16:58











  • I got asked this by someone. Why is it uncommon in Angular to do this? I expected that I can use some kind of CSS selector within the select.

    – Robin
    Nov 13 '18 at 17:00






  • 1





    usually you should not use any selector to get html element in your typescript code, but OPPOSITE - usually inside your html template you use your typescript objects by using directives like *ngIf, *ngFor,, (click)="someFunction(xx)" [(ngModule)]...

    – Kamil Kiełczewski
    Nov 13 '18 at 17:19







  • 1





    so usually trying to get some html-nodes inside .ts file is wrong approach to angular... The right approach is to use javascript (typescript) object inside template (not template elements inside javascript)

    – Kamil Kiełczewski
    Nov 13 '18 at 17:20


















  • for me is very exotic to use <div id=...> in angular template - can you explain why you wanna use it?

    – Kamil Kiełczewski
    Nov 13 '18 at 16:58











  • I got asked this by someone. Why is it uncommon in Angular to do this? I expected that I can use some kind of CSS selector within the select.

    – Robin
    Nov 13 '18 at 17:00






  • 1





    usually you should not use any selector to get html element in your typescript code, but OPPOSITE - usually inside your html template you use your typescript objects by using directives like *ngIf, *ngFor,, (click)="someFunction(xx)" [(ngModule)]...

    – Kamil Kiełczewski
    Nov 13 '18 at 17:19







  • 1





    so usually trying to get some html-nodes inside .ts file is wrong approach to angular... The right approach is to use javascript (typescript) object inside template (not template elements inside javascript)

    – Kamil Kiełczewski
    Nov 13 '18 at 17:20

















for me is very exotic to use <div id=...> in angular template - can you explain why you wanna use it?

– Kamil Kiełczewski
Nov 13 '18 at 16:58





for me is very exotic to use <div id=...> in angular template - can you explain why you wanna use it?

– Kamil Kiełczewski
Nov 13 '18 at 16:58













I got asked this by someone. Why is it uncommon in Angular to do this? I expected that I can use some kind of CSS selector within the select.

– Robin
Nov 13 '18 at 17:00





I got asked this by someone. Why is it uncommon in Angular to do this? I expected that I can use some kind of CSS selector within the select.

– Robin
Nov 13 '18 at 17:00




1




1





usually you should not use any selector to get html element in your typescript code, but OPPOSITE - usually inside your html template you use your typescript objects by using directives like *ngIf, *ngFor,, (click)="someFunction(xx)" [(ngModule)]...

– Kamil Kiełczewski
Nov 13 '18 at 17:19






usually you should not use any selector to get html element in your typescript code, but OPPOSITE - usually inside your html template you use your typescript objects by using directives like *ngIf, *ngFor,, (click)="someFunction(xx)" [(ngModule)]...

– Kamil Kiełczewski
Nov 13 '18 at 17:19





1




1





so usually trying to get some html-nodes inside .ts file is wrong approach to angular... The right approach is to use javascript (typescript) object inside template (not template elements inside javascript)

– Kamil Kiełczewski
Nov 13 '18 at 17:20






so usually trying to get some html-nodes inside .ts file is wrong approach to angular... The right approach is to use javascript (typescript) object inside template (not template elements inside javascript)

– Kamil Kiełczewski
Nov 13 '18 at 17:20













1 Answer
1






active

oldest

votes


















1














I can't give a reason why you can't target an ID using the syntax like #myID, but you are able to target attributes on DOM nodes using a syntax like [attribute=value] (like you are able to in CSS selectors). Using this, you can then target for a specific ID attribute. In your case you would want to use the following:



<ng-content select="[id=myID]"></ng-content>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285922%2fis-it-possible-to-select-by-id-in-angulars-ng-content%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    I can't give a reason why you can't target an ID using the syntax like #myID, but you are able to target attributes on DOM nodes using a syntax like [attribute=value] (like you are able to in CSS selectors). Using this, you can then target for a specific ID attribute. In your case you would want to use the following:



    <ng-content select="[id=myID]"></ng-content>





    share|improve this answer



























      1














      I can't give a reason why you can't target an ID using the syntax like #myID, but you are able to target attributes on DOM nodes using a syntax like [attribute=value] (like you are able to in CSS selectors). Using this, you can then target for a specific ID attribute. In your case you would want to use the following:



      <ng-content select="[id=myID]"></ng-content>





      share|improve this answer

























        1












        1








        1







        I can't give a reason why you can't target an ID using the syntax like #myID, but you are able to target attributes on DOM nodes using a syntax like [attribute=value] (like you are able to in CSS selectors). Using this, you can then target for a specific ID attribute. In your case you would want to use the following:



        <ng-content select="[id=myID]"></ng-content>





        share|improve this answer













        I can't give a reason why you can't target an ID using the syntax like #myID, but you are able to target attributes on DOM nodes using a syntax like [attribute=value] (like you are able to in CSS selectors). Using this, you can then target for a specific ID attribute. In your case you would want to use the following:



        <ng-content select="[id=myID]"></ng-content>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 17:11









        Daniel W StrimpelDaniel W Strimpel

        3,3961617




        3,3961617



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285922%2fis-it-possible-to-select-by-id-in-angulars-ng-content%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3