【问题标题】:Angular: Writing Logic into bindingAngular:将逻辑写入绑定
【发布时间】:2018-09-12 23:09:08
【问题描述】:

我正在尝试制定一些逻辑来绑定我的值,以便始终显示必要的值。我现在遇到的问题是我绑定到 theorem.name 但有时这不存在,在这种情况下我需要显示 theorem.description 。执行此逻辑的最佳方法是什么,所说的逻辑是什么?我的代码如下:

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();
  }
}

所以现在在我的 component.html 中带有标签“证明:”的 select 语句中,我将底部的每个选项值设置为 {{theorem.rule}}:{{theorem.name}}。但是,在某些情况下 {{theorem.name}} 是空的,在这种情况下我想显示 {{theorem.description}} 。最好的方法是什么?如何做到这一点?

【问题讨论】:

    标签: javascript html angular binding ngfor


    【解决方案1】:

    尝试使用

    {{ !theorem.name ? theorem.description : theorem.name}}
    

    【讨论】:

      【解决方案2】:

      这很简单:您可以使用||(“或”运算符)。

      {{theorem.name || theorem.description}}
      

      【讨论】:

        【解决方案3】:

        请查看代码更改

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

        【讨论】:

          【解决方案4】:

          尝试三进制检查 theorem.name 是否定义为 no,如果没有则显示 theorem.description:

          {{ theorem.name ? theorem.name : theorem.description}}
          

          【讨论】:

            猜你喜欢
            • 2012-12-28
            • 2013-05-09
            • 1970-01-01
            • 1970-01-01
            • 2016-06-28
            • 1970-01-01
            • 1970-01-01
            • 2023-03-18
            • 1970-01-01
            相关资源
            最近更新 更多