【发布时间】:2021-09-11 00:18:50
【问题描述】:
我目前正在处理一个 Angular 项目,我的文件最初看起来如下:
dog.ts:
export interface Dog {
name: string;
age: number;
breed: string;
}
dog.component.ts:
import { Dog } from '../dog';
@Component({
//setup stuff
})
export class DogComponent implements OnInit {
dog: Dog = {
name: "",
age: 0,
breed: ""
};
constructor() {}
method(dog: Dog): void {
//methods with headers similar to above that use the passed dog object
}
dog.component.html:
<div class="input">
<div>
<label for="name">Name: </label>
<input id="name" [(ngModel)]="dog.name" placeholder="name">
</div>
<div>
<label for="age">Age: </label>
<input id="age" [(ngModel)]="dog.age" placeholder=0>
</div>
<div>
<label for="breed">Breed: </label>
<input id="breed" [(ngModel)]="dog.breed" placeholder="breed">
</div>
</div>
<button class="method" (click)="method(dog)">
Use Method
</button>
<!--more buttons calling various functions -->
但是,我现在正在删除 .html 文件中的输入字段,并将它们替换为不同模型的单个输入,如下所示:
dog.component.html:
<div class="input">
<div>
<label for="place">Name: </label>
<input id="place" [(ngModel)]="ranking.place" placeholder="place">
</div>
</div>
<!-- Same buttons as before -->
然后我创建了一个服务,给定一个排名对象,它将发出一个 GET http 请求并返回一个 dog 对象。所以我现在想在我的 dog.component.ts 文件中创建一个方法,在给定排名时更新狗模型。所以是这样的:
dog.component.ts:
import { Dog } from '../dog';
import { Ranking } from '../ranking';
@Component({
//setup stuff
})
export class DogComponent implements OnInit {
dog: Dog = {
name: "",
age: 0,
breed: ""
};
ranking: Ranking = {
place: 0,
};
constructor() {}
updateDogComponenet(ranking: Ranking): void {
//FILL IN THIS METHOD
//insert a call to serviceFunction that takes a ranking and returns a dog
//update the ngModel dog object so that it can be used in other functions
}
method(dog: Dog): void {
//methods with headers similar to above that use the passed dog object
}
我为所有代码道歉,但如果有人有任何想法,将不胜感激!
【问题讨论】:
标签: javascript html node.js angular typescript