首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度:将逻辑写入绑定

角度:将逻辑写入绑定
EN

Stack Overflow用户
提问于 2018-04-03 14:58:35
回答 3查看 178关注 0票数 3

我试图为绑定我的值建立一个小逻辑,以便始终显示必要的值。我现在遇到的问题是,我绑定到theorem.name,但有时并不存在,在这种情况下,我需要显示theorem.description。做这个逻辑最好的方法是什么?说逻辑是什么?我的代码如下:

editor-form.component.html

代码语言:javascript
复制
<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

代码语言:javascript
复制
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.name:”,我正在将底部的每个选项值设置为{theorem.rule}:{theorem.name}}。但是,在某些情况下{{theorem.name}}是空的,在这种情况下,我希望显示{theorem.description}}。做这件事的最好方法是什么,怎么做呢?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-04-03 15:04:42

这很简单:您可以只使用|| (“或”操作符)。

代码语言:javascript
复制
{{theorem.name || theorem.description}}
票数 0
EN

Stack Overflow用户

发布于 2018-04-03 15:03:27

请查看代码更改情况。

代码语言:javascript
复制
    <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>
票数 1
EN

Stack Overflow用户

发布于 2018-04-03 15:05:40

试着使用

代码语言:javascript
复制
{{ !theorem.name ? theorem.description : theorem.name}}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49633204

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档