Angular 2 插值将数字转换为字符串
Angular 2 interpolation converts number to string
我有以下组件,在 onClick 事件中我试图 increment/decrement 其计数字段的值。但是,当我这样做时,它将计数字段转换为字符串。我知道插值将所有内容都转换为字符串,但它不应该将字符串转换为正确的类型,因为我使用的是那个字段吗?我在这里遗漏了什么吗?
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'like',
template: `<i class="glyphicon glyphicon-heart" [class.liked]="isLiked" (click)="onClick()"></i><span>{{count}}</span>`,
styleUrls: ['./like.component.css']
})
export class LikeComponent implements OnInit {
@Input() isLiked: boolean;
@Input() count: number;
@Output() change = new EventEmitter();
constructor() { }
onClick() {
console.log(typeof this.count); // <--- string
this.isLiked = !this.isLiked;
this.count = Number(this.count) + (this.isLiked ? 1 : -1);
this.change.emit({value: this.isLiked});
}
ngOnInit() {
}
}
<like count="10"></like>
就像
<like [count]="'10'"></like>
也一样
<like [count]="10"></like>
我有以下组件,在 onClick 事件中我试图 increment/decrement 其计数字段的值。但是,当我这样做时,它将计数字段转换为字符串。我知道插值将所有内容都转换为字符串,但它不应该将字符串转换为正确的类型,因为我使用的是那个字段吗?我在这里遗漏了什么吗?
import {Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
@Component({
selector: 'like',
template: `<i class="glyphicon glyphicon-heart" [class.liked]="isLiked" (click)="onClick()"></i><span>{{count}}</span>`,
styleUrls: ['./like.component.css']
})
export class LikeComponent implements OnInit {
@Input() isLiked: boolean;
@Input() count: number;
@Output() change = new EventEmitter();
constructor() { }
onClick() {
console.log(typeof this.count); // <--- string
this.isLiked = !this.isLiked;
this.count = Number(this.count) + (this.isLiked ? 1 : -1);
this.change.emit({value: this.isLiked});
}
ngOnInit() {
}
}
<like count="10"></like>
就像
<like [count]="'10'"></like>
也一样
<like [count]="10"></like>