Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular: Writing Logic into binding

I am trying to make a little logic for binding my values so that the necessary value is always displayed. The problem I have right now is I am binding to theorem.name but sometimes this does not exist and in this case I need to display theorem.description instead. What is the best way to do this logic and what would said logic be? My code is as below:

editor-form.component.html

<div *ngIf="!infoFilled">
  <form>
    <fieldset>
      <legend>Editor Form</legend>
      <div class="form-group">
        <label>Name:</label>
        <input [(ngModel)]="nameText" type="text" name="proof" value="" class="form-control">
      </div>
      <div class="form-group">
        <label>Class:</label>
        <input [(ngModel)]="classText" type="text" name="class" value="" class="form-control">
      </div>
      <div class="form-group">
        <label>Proof:</label>
        <select (change)="onProofSelectionChanged($event.target.value)" [(ngModel)]="proofText" name="proofs" class="form-control">
          <option style="display:none" value=""></option>
          <option value="custom">[Custom]</option>
          <option *ngFor="let theorem of (theorems$ | async)" [ngValue]="'(' + theorem.rule + ') ' + theorem.name">
            {{theorem.rule}}: {{theorem.name}}
          </option>
        </select>
        <input [hidden]="!customProofSelected" type="text" class="form-control">
      </div>
      <div class="form-group">
        <label>Description:</label>
        <textarea
          [(ngModel)]="descriptionText"
          name="description"
          cols="30" rows="5"
          class="form-control"
          placeholder="Proof by mathematical induction... "></textarea>
      </div>
    </fieldset>
    <button (click)="formSubmit()" class="btn btn-primary">Generate Editor</button>
  </form>
</div>

editor-form.component.ts

import {Component, OnDestroy, OnInit} from '@angular/core';
import {EditorService} from '../editor/editor.service';
import {BibleService} from '../bible/bible.service';
import {Theorem} from '../../model/theorem';
import {Observable} from 'rxjs/Observable';

@Component({
  selector: 'app-editor-form',
  templateUrl: './editor-form.component.html',
  styleUrls: ['./editor-form.component.scss']
})
export class EditorFormComponent implements OnInit, OnDestroy {

  nameText = '';
  classText = '';
  proofText = '';
  descriptionText = '';
  infoFilled: boolean;
  infoFilledSubscription;
  customProofSelected = false;
  theorems$: Observable<Theorem[]>;

  constructor(private editorService: EditorService, private bibleService: BibleService) {
    this.infoFilledSubscription = this.editorService.infoFilledChange.subscribe(infoFilled => {
      this.infoFilled = infoFilled;
    });
  }

  formSubmit() {
    this.editorService.toggleFormFilled();
    const outline =
      ('Name: ').bold() +  this.nameText + '<br />' +
      ('Class: ').bold() + this.classText + '<br />' +
      ('Proof: ').bold() + this.proofText + '<br /><br />' +
      ('Solution: ').bold() +  '<br />' +
      this.descriptionText;
    this.editorService.submitData(outline);
  }

  onProofSelectionChanged(selection) {
    if (selection === 'custom') {
      this.customProofSelected = true;
    } else {
      this.customProofSelected = false;
    }
  }

  ngOnInit() {
    this.theorems$ = this.bibleService.findAllTheorems();
  }

  ngOnDestroy() {
    this.infoFilledSubscription.unsubscribe();
  }
}

So right now in my component.html in the select statement with the label "Proof:" I am setting each option value at the bottom to {{theorem.rule}} : {{theorem.name}}. But, in some cases {{theorem.name}} is empty, and in this case I want to display {{theorem.description}} instead. What is the best way to do this and how can it be done?

like image 437
FrankTheTank Avatar asked Mar 28 '26 23:03

FrankTheTank


1 Answers

please take a look of code changes

    <div *ngIf="!infoFilled">
  <form>
    <fieldset>
      <legend>Editor Form</legend>
      <div class="form-group">
        <label>Name:</label>
        <input [(ngModel)]="nameText" type="text" name="proof" value="" class="form-control">
      </div>
      <div class="form-group">
        <label>Class:</label>
        <input [(ngModel)]="classText" type="text" name="class" value="" class="form-control">
      </div>
      <div class="form-group">
        <label>Proof:</label>
        <select (change)="onProofSelectionChanged($event.target.value)" [(ngModel)]="proofText" name="proofs" class="form-control">
          <option style="display:none" value=""></option>
          <option value="custom">[Custom]</option>
          <option *ngFor="let theorem of (theorems$ | async)" [ngValue]="'(' + theorem.rule + ') ' + theorem.name">
            {{theorem.rule}}: **<span *ngIf="theorem.name">{{theorem.name}}</span><span *ngIf="!theorem.name">{{theorem.description}}</span>**
          </option>
        </select>
        <input [hidden]="!customProofSelected" type="text" class="form-control">
      </div>
      <div class="form-group">
        <label>Description:</label>
        <textarea
          [(ngModel)]="descriptionText"
          name="description"
          cols="30" rows="5"
          class="form-control"
          placeholder="Proof by mathematical induction... "></textarea>
      </div>
    </fieldset>
    <button (click)="formSubmit()" class="btn btn-primary">Generate Editor</button>
  </form>
</div>
like image 195
Kalleshwar Kalshetty Avatar answered Mar 31 '26 04:03

Kalleshwar Kalshetty