gpt4 book ai didi

javascript - Angular:将逻辑写入绑定(bind)

转载 作者:行者123 更新时间:2023-11-29 17:46:04 27 4
gpt4 key购买 nike

我正在尝试制定一些逻辑来绑定(bind)我的值,以便始终显示必要的值。我现在遇到的问题是我绑定(bind)到 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}}。做到这一点的最佳方法是什么以及如何完成?

最佳答案

请查看代码更改

    <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>

关于javascript - Angular:将逻辑写入绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49633204/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com