以下是代码:
HTML:
car = new Car();
ngOnInit(){
// eg: I get the car.name from here. The car.name = "BMW";
this.http.get('/data').subscribe(car => this.car = car);
}
save(){
// Here how I compare the car.name with "BMW" ?
}
class Car{
constructor(private string name){}
}
TS:
car2 = new Car();
save() {
this.car2 = JSON.parse(JSON.stringify(car));
if(car.name == car2.name)
console.log("the name is BWM");
}else{
console.log("the name isn't BWM");
}
car.name总是更改,因为我使用的是ngModel。 我想将它与第一个值“BWM”进行比较。 我正在学习角形。如何使用它?或使用Rxjs?
我不能接受这个:
:tellWithCard
答案 0 :(得分:0)
因此,如果您想将旧值与最新值进行比较,请将输入添加到输入中,如
[(ngModel)]="myModel" #newvalue (change)="changeEvent(newvalue.value)"
和打字稿
changeEvent(newValue) {
console.log(newValue, this.myModel);
}
希望这会有所帮助。使用角度模板参考变量here 找到详细信息
答案 1 :(得分:0)
我会说使用分离的对象:
HTML:
...
<input type="text" [(ngModel)]="inputCarName"/>
...
&#13;
TS:
car = new Car();
inputCarName: string;
ngOnInit(){
// eg: I get the car.name from here. The car.name = "BMW";
this.http.get('/data').subscribe(car => this.car = car);
}
save(){
// Here how I compare the car.name with "BMW" ?
if(inputCarName == this.car.name)
{
//...whatever
}
}
class Car{
constructor(private string name){}
}
&#13;