为什么我不能以相同 class 的方法访问字段

Why cannot I access field in a method of same class

以下是我的组件代码的一部分

export class RootComponent{
iSActive = true;

  setVal(j){
     if(j==0){
        isActive = false;
     }
  }

}

我是 Angular 的新手,我的问题听起来可能很傻。我的问题是为什么我不能在相同 class 的方法中 use/set 字段 isActive,例如 setVal.

使用访问以下variable/field

export class rootComponent{
   iSActive = true;

   setVal(j){
    if(j==0){
    this.iSActive = false;
    }
 }

}

为了在 class 函数中更改 class 级别变量的值,您需要使用 this 关键字。 this 是用来告诉函数在更改变量值时要使用的范围。没有它,作用域实际上就在函数本身内部。

示例:this.isActive = false;