Skip to content

Support template content for selected value in md-select #2275

Closed

Description

@wouterbeukers

Bug, feature request, or proposal:

bug

What is the expected behavior?

Selected option of a md-select supports html-tags

What is the current behavior?

Selected option of a md-select doesn't supports html-tags

What are the steps to reproduce?

See http://plnkr.co/edit/QJUCobw9yFcu1l1WtpOR?p=preview
Open the md-select.
In the dropdown the option supports html tags
Select the option
All html tags are stripped of the shown value in the md-select.

Activity

  1. dougmbarcellos commented on Dec 22, 2016

    @dougmbarcellos

    Use this to display the formatted html:
    <div [innerHtml]="selectedValue"></div>

  2. wouterbeukers commented on Dec 22, 2016

    @wouterbeukers
    Author

    That isn't the issue.
    It is about the value what the md-selects shows after you select an option.
    This value is stripped of al it's html-tags.

  3. negberts commented on Dec 27, 2016

    @negberts

    The html is stripped here:

    get viewValue(): string {
    // TODO(kara): Add input property alternative for node envs.
    return this._getHostElement().textContent.trim();
    }

    I would love to change it, but I don't know what the impact will be. There's probably a reason why textContent is taken and not just value

  4. added
    feature
    on Feb 10, 2017
  5. fxck commented on Feb 28, 2017

    @fxck
    Contributor

    @kara do you have something in mind regarding this? I would probably like to take a look at it.. also @crisbeto do you expect any problems with this because of multiselect?

  6. crisbeto commented on Feb 28, 2017

    @crisbeto
    Member

    I don't expect any issues with multi select since it'll use the same property to get the text. I was actually considering giving this a shot in the next few days.

  7. added a commit that references this issue on Feb 28, 2017
    4be049b
  8. andreasrueedlinger commented on Mar 7, 2017

    @andreasrueedlinger

    @crisbeto thanks for your PR. So I guess you intended that with this change you can add the "plain HTML" option as label as follows:
    <md-select-label [innerHtml]="select.selected?._getHostElement().innerHTML"></md-select-label>

  9. crisbeto commented on Mar 7, 2017

    @crisbeto
    Member

    This is the intended usage @andreasrueedlinger:

    <md-select #select="mdSelect">
      <md-select-label>
        {{ select.selected?.viewValue }}
      </md-select-label>
      <!-- options go here -->
    </md-select>
  10. andreasrueedlinger commented on Mar 7, 2017

    @andreasrueedlinger

    @crisbeto but that does not fix the problem adressed in this issue, namely that the label does not support Html!

  11. crisbeto commented on Mar 7, 2017

    @crisbeto
    Member

    You can put any HTML you want inside the md-select-label element.

  12. fxck commented on Mar 7, 2017

    @fxck
    Contributor

    That's not what he meant I think.

    When you have..

    <md-option>
      <img src="whatever.jpg" /> Whatever
    </md-option>

    ..and you select it, the selected content won't be <img src="whatever.jpg" /> Whatever, it will be stripped down to Whatever. He wants the original unstripped HTML to be there instead. I'm not sure if {{ select.selected?.viewValue }} does that?

  13. andreasrueedlinger commented on Mar 7, 2017

    @andreasrueedlinger

    But in your example you use normal binding and viewValue which returns the trimmed textContent, so no Html anymore.
    My example:

    <md-select placeholder="Feedback" [formControl]="control" #select="mdSelect">
      <md-select-label [innerHtml]="select.selected?._getHostElement().innerHTML"></md-select-label>
      <md-option value="good">
        <md-icon>thumb_up</md-icon> Nice!
      </md-option>
      <md-option value="bad">
        <md-icon>thumb_down</md-icon> Bad!
      </md-option>
    </md-select>
  14. 12 remaining items

  15. added
    P4
    on Jun 6, 2017
  16. negberts commented on Jun 24, 2017

    @negberts

    An updated plunker for the problem can be found here: http://plnkr.co/edit/vuF3Pz7UHDXERXZbtiIK?p=preview

  17. mladilav commented on Jul 6, 2017

    @mladilav

    Any news about this?

  18. sdargaud commented on Jul 14, 2017

    @sdargaud

    As a dirty workaround I am currently using somehting along those lines :

    <md-select placeholder="Feedback" [formControl]="control" #select="mdSelect">
      <md-option value="good">
        <md-icon *ngIf="select.panelOpen">thumb_up</md-icon> Nice!</md-icon>
        <div *ngIf="!select.panelOpen">Nice!</div>
      </md-option>
    </md-select>
    

    At least I don't have "thumb_up Nice!" displayed and can work with it until it's fixed (be aware, it does seem to take some time to evaluate the template when opening the select, and the raw html is shown for a short period of time).

  19. added a commit that references this issue on Aug 8, 2017
    e3cdb21
  20. added a commit that references this issue on Aug 8, 2017
    1124a30
  21. added a commit that references this issue on Aug 9, 2017
    72c5d39
  22. LayZeeDK commented on Nov 22, 2018

    @LayZeeDK
    Contributor

    For future reference, you can use the <mat-select-trigger> to change the display of the selected value.

    It was added by @crisbeto in Angular Material version 2 beta 10.

  23. angular-automatic-lock-bot commented on Sep 10, 2019

    @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.

  24. locked and limited conversation to collaborators on Sep 10, 2019
  25. added
    needs: discussion
    on Mar 3, 2020
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

    P4featureneeds: discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions