Angular 4 ng-selected not working

XXIV

I cannot get ng-selected to work. I first tried just adding selected in the option tag, but after reading it seems like I need to use ng-select. I've tried doing ng-selected="true" and ng-selected="selected" with no luck. I've tried doing the recommended fixes from other stackeoverflow questions but none seem to work for me. At one point (in another html form bc I am having this same problem) it seemed to work, but now it magically stopped.

<div class="col-lg-6">
  <div class="panel panel-default">
    <div class="panel-heading">Caloric Intake Recommendation</div>
    <div class="panel-body">
      <form [formGroup]="myForm" (ngSubmit)="onSubmit()">
        <div class="form-group row">
          <label for="weight" class="col-lg-2 col-form-label">Weight</label>
          <div class="col-lg-10">
            <input
                type="number"
                class="form-control"
                id="weight"
                formControlName="weight"
                placeholder="Weight">
          </div>
        </div>
        <div class="form-group row">
          <label for="goal" class="col-lg-2 col-form-label">Goal</label>
          <div class="col-lg-10">
            <select class="form-control" formControlName="goal" id="goal">
              <option ng-selected="selected">Lose Weight</option>
              <option>Maintain Weight</option>
              <option>Gain Mass</option>
            </select>
          </div>
        </div>
        <button
            class="btn btn-primary"
            type="submit"
            [disabled]="!myForm.valid">Submit
        </button>
        <hr>
        <div class="col-sm-4">
          <label class="col-sm-2 col-form-label">Your Recommended Protein:</label>
          <input type="text" value={{this.protein}}>
        </div>
        <div class="col-sm-4">
          <label class="col-sm-2 col-form-label">Your Recommended Carbs:</label>
          <input type="text" value={{this.carbs}}>
        </div>
        <div class="col-sm-4">
          <label class="col-sm-2 col-form-label">Your Recommended Fats:</label>
          <input type="text" value={{this.fat}}>
        </div>
      </form>
    </div>
  </div>
</div>

Component

import {Component} from "@angular/core";
import {FormControl, FormGroup, Validators} from "@angular/forms";
import {CaloricIntakeClass} from "./caloric-intake.class";

@Component({
  selector: 'app-caloric-intake',
  templateUrl: './caloric-intake.component.html',
  styleUrls: ['./caloric-intake.component.css']
})
export class CaloricIntakeComponent{
  myForm: FormGroup;
  caloricIntake: CaloricIntakeClass;
  weight: number;
  goal: string;
  protein: number;
  carbs: number;
  fat: number;


  onSubmit() {
    this.weight = this.myForm.value.weight;
    this.goal = this.myForm.value.goal;
    this.myForm.reset();

    this.caloricIntake = new CaloricIntakeClass(this.weight);
    if (this.goal === 'Cutting'){
          this.caloricIntake.cuttingIntake();
    } else if (this.goal === 'Bulking'){
          this.caloricIntake.bulkingIntake();
    } else {
          this.caloricIntake.maintaingIntake();
    }
    this.protein = this.caloricIntake.getProtein();
    this.carbs = this.caloricIntake.getCarbs();
    this.fat = this.caloricIntake.getFat();
  }

  ngOnInit() {
    this.myForm = new FormGroup({
      weight: new FormControl(null, Validators.required),
      goal: new FormControl(null, Validators.required)
    });
  }
}
Daniel H.J.

In your template, your option's ng-selected is bound to a variable called 'selected'. That means in your component, you need to set this variable to true at the beginning, something like this:

export class CaloricIntakeComponent{

    selected : boolean;

    ......

    ngOnInit() {
        this.selected = true;
        ......

    }

}


Update

Make sure you have values in the options, like this:

<select class="form-control" formControlName="goal" id="goal">
    <option value="lose_weight">Lose Weight</option>
    <option value="maintain_weight">Maintain Weight</option>
    <option value="gain_mass">Gain Mass</option>
</select>

Then, you do not need ng-selected at all. You can just initiate with your custom values in the component.

ngOnInit() {
    this.myForm = new FormGroup({
        weight: new FormControl(null, Validators.required),
        goal: new FormControl("lose_weight", Validators.required)
    });
}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Angular ng If not working with razor syntax

分類Dev

Angular 4 ng-content select not working on dynamically created transcluded elements

分類Dev

Angular 4 ng-show

分類Dev

Angular ng2-opd-popup not working

分類Dev

angular orderBy in ng-repeat not working

分類Dev

why ng-plurazlize not working in angular js?

分類Dev

angular input ng-bind not working

分類Dev

Angular js ng-repeat and ng-show not working on refresh

分類Dev

RegEx not working in directive - Angular 4

分類Dev

angular-cli "ng build" doesn't produce a working project?

分類Dev

A directive to detect $last is not working with ng-if in angular.js

分類Dev

ng new has stopped working after updating to Angular 6

分類Dev

In Angular ng-bootstrap modal with template, close and dismiss buttons not working

分類Dev

Angular ng-include not working with laravel views : blade template

分類Dev

ng2-Chart not working with newly created angular 10 project

分類Dev

Angular ng-show not working on initial page load

分類Dev

Angular's ng include directive not working for the expected link

分類Dev

Angular Directive scope binding not working in ng-repeat

分類Dev

Angular: need search bar working on 2 different ng-repeat

分類Dev

routerlinkactive not working for angular 4 home button

分類Dev

Angular 4 - Populate form with JSON data is not working

分類Dev

show selected Drop down value in same page in angular4

分類Dev

angular 6 ng-select how to set item selected in drop down list by using template expression

分類Dev

Angular 4ng-repeatの実装

分類Dev

ng2-charts - Angular 4 の作業例

分類Dev

Php select=selected not working

分類Dev

ionic3/angular 4 : Injecting a service in another service not working

分類Dev

I don't have any error but the search is not working Angular 4

分類Dev

filter orderby is not working as expected for values greater than 10 in angular 4

Related 関連記事

  1. 1

    Angular ng If not working with razor syntax

  2. 2

    Angular 4 ng-content select not working on dynamically created transcluded elements

  3. 3

    Angular 4 ng-show

  4. 4

    Angular ng2-opd-popup not working

  5. 5

    angular orderBy in ng-repeat not working

  6. 6

    why ng-plurazlize not working in angular js?

  7. 7

    angular input ng-bind not working

  8. 8

    Angular js ng-repeat and ng-show not working on refresh

  9. 9

    RegEx not working in directive - Angular 4

  10. 10

    angular-cli "ng build" doesn't produce a working project?

  11. 11

    A directive to detect $last is not working with ng-if in angular.js

  12. 12

    ng new has stopped working after updating to Angular 6

  13. 13

    In Angular ng-bootstrap modal with template, close and dismiss buttons not working

  14. 14

    Angular ng-include not working with laravel views : blade template

  15. 15

    ng2-Chart not working with newly created angular 10 project

  16. 16

    Angular ng-show not working on initial page load

  17. 17

    Angular's ng include directive not working for the expected link

  18. 18

    Angular Directive scope binding not working in ng-repeat

  19. 19

    Angular: need search bar working on 2 different ng-repeat

  20. 20

    routerlinkactive not working for angular 4 home button

  21. 21

    Angular 4 - Populate form with JSON data is not working

  22. 22

    show selected Drop down value in same page in angular4

  23. 23

    angular 6 ng-select how to set item selected in drop down list by using template expression

  24. 24

    Angular 4ng-repeatの実装

  25. 25

    ng2-charts - Angular 4 の作業例

  26. 26

    Php select=selected not working

  27. 27

    ionic3/angular 4 : Injecting a service in another service not working

  28. 28

    I don't have any error but the search is not working Angular 4

  29. 29

    filter orderby is not working as expected for values greater than 10 in angular 4

ホットタグ

アーカイブ