Javascript 角度:将逻辑写入绑定
我试图为绑定我的值创建一个小逻辑,以便始终显示必要的值。我现在的问题是我绑定到定理.name,但有时这并不存在,在这种情况下,我需要显示定理.description。做这个逻辑的最好方法是什么?所说的逻辑是什么?我的代码如下: 编辑器-form.component.html 编辑表单 姓名: 类别: 证明: [海关] {{定理.规则}:{{定理.名称} 说明: 生成编辑器Javascript 角度:将逻辑写入绑定,javascript,html,angular,binding,ngfor,Javascript,Html,Angular,Binding,Ngfor,我试图为绑定我的值创建一个小逻辑,以便始终显示必要的值。我现在的问题是我绑定到定理.name,但有时这并不存在,在这种情况下,我需要显示定理.description。做这个逻辑的最好方法是什么?所说的逻辑是什么?我的代码如下: 编辑器-form.component.html 编辑表单 姓名: 类别: 证明: [海关] {{定理.规则}:{{定理.名称} 说明: 生成编辑器 尝试检查定理.name是否定义为“否”,如果没有,则显示定理.description: {{ theorem.name ?
尝试检查定理.name是否定义为“否”,如果没有,则显示定理.description:
{{ theorem.name ? theorem.name : theorem.description}}
请看一下代码更改
<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>
这很简单:您可以使用| |或运算符
{{theorem.name || theorem.description}}
尝试使用
{{ !theorem.name ? theorem.description : theorem.name}}