我试图为绑定我的值建立一个小逻辑,以便始终显示必要的值。我现在遇到的问题是,我绑定到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.name:”,我正在将底部的每个选项值设置为{theorem.rule}:{theorem.name}}。但是,在某些情况下{{theorem.name}}是空的,在这种情况下,我希望显示{theorem.description}}。做这件事的最好方法是什么,怎么做呢?
发布于 2018-04-03 15:04:42
这很简单:您可以只使用|| (“或”操作符)。
{{theorem.name || theorem.description}}发布于 2018-04-03 15:03:27
请查看代码更改情况。
<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>发布于 2018-04-03 15:05:40
试着使用
{{ !theorem.name ? theorem.description : theorem.name}}https://stackoverflow.com/questions/49633204
复制相似问题