我正在尝试为绑定我的值制定一些逻辑,以便始终显示必要的值。我现在遇到的问题是我绑定到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中带有标签“ Proof:”的select语句中,我将底部的每个选项值设置为{{theorem.rule}}:{{theorem.name}}。但是,在某些情况下,{{theorem.name}}为空,在这种情况下,我想显示为{{theorem.description}}。最好的方法是什么,怎么做?
最佳答案
这非常简单:您可以只使用||
(“或”运算符)。
{{theorem.name || theorem.description}}