Mình đang thử dùng angular service, sao cái kết quả này ngộ thế?
##This is service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class ToanService {
constructor() { }
cong(a: number, b: number): number {
return a + b;
}
}
##This is component.ts
import { Component, Input } from '@angular/core';
import { ToanService } from './toan.service';
@Component({
selector: 'hello-use-service',
template:
`
<div class="box2">
this is hello use service component<br>
{{name}}<br>
{{ten}}<br>
a : <input [(ngModel)]="a" /><br>
a : <input [(ngModel)]="b" /><br>
<button (click)="on_cong()" >Cộng</button>
</div>
`,
styles: [
'.box{border:1px solid blue}',
'.box2{border:1px solid green;padding:5px;margin:5px;}'
]
})
export class HelloUseServiceComponent {
@Input() name: string;
ten = "thuc 101";
a = 1;
b = 1;
constructor(private myservice: ToanService) { }
ngOnInit() {
}
on_cong() {
var tong = this.myservice.cong(this.a, this.b);
alert(tong);
}
}
Sao kết quả là nối chuỗi: 1,1=>11; 111,1=>1111???