Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 HTML select menu set default value

I'm used to the older angularjs way of doing select menus and selecting the default value etc and am having trouble wrapping my head around how to do this in Angular (6).

I have a an array of menu items:

  fontChoices = [
    {
      label: 'Trebuchet',
      value: "'Trebuchet MS', 'Helvetica Neue', Arial, sans-serif"
    },
    {
      label: 'Georgia',
      value: 'Georgia, times, serif'
    }
 ];

and a variable to hold the chosen font: brandFont

My html menu is

<select [(ngModel)]="brandFont"
        id="brandFontmenu"
        (ngModelChange)="setStyle($event,'--font-family-brand')">
   <option *ngFor="let font of fontChoices"
           [value]="font.value">{{font.label}}
   </option>
</select>

The menu works, displays my fontChoices, and changing the selection fires my function. I can choose Georgia or Trebuchet and the css variable changes and the page updates as it should.

  setStyle(e, which) {
    document.documentElement.style.setProperty(which, e);
  }

On page load, the css variable is set to Trebuchet. Which I can get in ngOnInit with

this.brandFont = String(styles.getPropertyValue('--font-family-brand')).trim();

My question is, how do I get the select menu to display this first choice on page load? I have tried this.brandFont = this.fontChoices[0]; but the menu selected item is empty until something is chosen from the menu.

like image 598
Steve Avatar asked Jun 26 '18 16:06

Steve


People also ask

How to set default value in HTML select tag?

The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.

How do I select a default dropdown in angular 6?

You can do this: <select class='form-control' (change)="ChangingValue($event)" [value]='46'> <option value='47'>47</option> <option value='46'>46</option> <option value='45'>45</option> </select> // Note: You can set the value of select only from options tag.

How do I set default selected value in ng options?

Use ng-init to set default value for ng-options . Save this answer.

How do I create a dropdown value in TypeScript?

First, create the template variables for dropdown using #teams . Use the event binding to listen for the change event and link with the onSelect method. The onSelect method receives the reference variable and takes the value of the dropdown element. Next, create the onSelected(value) method in the TypeScript file.


1 Answers

Use an option with the first value like this <option [value]="defaultFont.label" selected="selected">{{defaultFont.label}}</option>

Component HTML

<select [(ngModel)]="brandFont"
        id="brandFontmenu"
        >
   <option [value]="defaultFont.label" selected="selected">{{defaultFont.label}}
   </option>
   <option *ngFor="let font of fontChoices.slice(1)"
           [value]="font.label">{{font.label}}
   </option>
</select>

Component ts

  brandFont: any; 
  defaultFont: any;

  ngOnInit() {
    this.defaultFont = this.fontChoices[0];
    this.brandFont = Object.assign(this.defaultFont.label);
  }

Here is a demo stackblitz

like image 109
Abel Valdez Avatar answered Sep 19 '22 15:09

Abel Valdez