我在试着理解这里发生了什么。我有两个变量activeToInactive
和inactiveToActive
,每当我更新状态时,我都会递增它们。首先是我的代码
柜台服务:
export class CounterService {
// Initial States
activeToInactive = 0
inactiveToActive = 0
incrementStatusCount(statusCount: number) {
// Increment either of the initial states whenever the function is called.
statusCount++
if (statusCount == this.activeToInactive) {
console.log(`${statusCount} - active to inactive`)
}
if (statusCount == this.inactiveToActive) {
console.log(`${statusCount} - inactive to aactive`)
}
// I notice that statusCount does equal the initial states so if statements evaluate to false
}
}
字符串
接送服务:
import { Injectable } from "@angular/core";
import { CounterService } from "./counter.service";
@Injectable()
export class transferService {
constructor(private Counter: CounterService) {}
activeUsers = ['Max', 'Anna'];
inactiveUsers = ['Chris', 'Manu']
setToInactive(id: number) {
this.inactiveUsers.push(this.activeUsers[id]);
this.activeUsers.splice(id, 1);
// Calling the function in a different service
this.Counter.incrementStatusCount(this.Counter.activeToInactive)
}
setToActive(id: number) {
this.activeUsers.push(this.inactiveUsers[id]);
this.inactiveUsers.splice(id, 1);
// Calling the function in a different service
this.Counter.incrementStatusCount(this.Counter.inactiveToActive)
}
}
型
调用Transfer Service中的任一函数时,初始状态都不会递增。我所知道的是statusCount
不等于任何初始状态。这和推荐信有关吗?我还没有掌握他们,但如果你有任何提示,我可以如何处理这一点,然后让我知道。
提前感谢!!!
1条答案
按热度按时间eimct9ow1#
你已经很接近了。
在
incrementStatusCount
函数中,您将接收一个参数statusCount
,您打算使用该参数根据传递给函数的值递增activeToInactive
或inactiveToActive
。但是,在
TransferService
中,当调用this.Counter.incrementStatusCount(this.Counter.activeToInactive)
和this.Counter.incrementStatusCount(this.Counter.inactiveToActive)
时,传递的是activeToInactive
和inactiveToActive
的初始值,而不是要递增的实际计数。所以我们直接使用inactiveToActive
和activeToInactive
。像这样更新您的CounterService:
字符串
您的TranserService只会稍微改变,如下所示:
型