我正在尝试为绑定我的值创建一个小逻辑,以便始终显示必要的值。我现在遇到的问题是我绑定到theorem.name但有时这不存在,在这种情况下我需要显示theorem.description。执行此逻辑的最佳方法是什么?所谓的逻辑是什么?我的代码如下:
主编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>
&#13;
主编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();
}
}
&#13;
现在,在我的component.html中,在select语句中使用标签&#34; Proof:&#34;我将底部的每个选项值设置为{{theorem.rule}}:{{theorem.name}}。但是,在某些情况下{{theorem.name}}是空的,在这种情况下我想显示{{theorem.description}}。这样做的最佳方法是什么?如何做到这一点?
答案 0 :(得分:1)
尝试三元检查theorem.name是否定义为no,如果没有显示定理。描述:
{{ theorem.name ? theorem.name : theorem.description}}
答案 1 :(得分:1)
请查看代码更改
<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>
答案 2 :(得分:1)
这非常简单:您只需使用||
(&#34;或&#34;运营商)。
{{theorem.name || theorem.description}}
答案 3 :(得分:1)
尝试使用
{{ !theorem.name ? theorem.description : theorem.name}}