简体   繁体   English

角递归组件阻止共享属性

[英]Angular recursive component prevent sharing properties

I have a recursive component to create a dynamically updating tree structure which can be collapsed. 我有一个递归组件来创建可以折叠的动态更新树结构。 However, it seems that the active variable which determines the state is shared with child components. 但是,似乎确定状态的active变量与子组件共享。 Is there any way to prevent this? 有什么办法可以防止这种情况?

@Component({
  selector: 'app-og-span',
  templateUrl: './og-span.component.html',
  styleUrls: ['./og-span.component.css']
})
export class OgSpanComponent  {
  @Input() comments;
  private isActive = true;

  toggleActive($event) {
    console.log('wtf');
    this.isActive = !this.isActive;
    $event.stopPropagation();
    return false;
  }
}

And the HTML is: HTML是:

<div class="ui accordion" style="padding-left: 15px" *ngFor="let comment of comments">
  <div class="title" [ngClass]="{'active': isActive}" (click)="toggleActive($event);$event.stopPropagation()">
    <i class="dropdown icon"></i>
    {{comment.text}}
  </div>
  <div class="content" [ngClass]="{'active': isActive}">
    <app-og-span [comments]="comment.comments" *ngIf="comment.comments"></app-og-span>
  </div>
</div>

I know the event is fired only once from logs. 我知道该事件仅从日志中触发一次。 There was a case of variable scopes with AngularJS (1.x) but I cannot find to find the corresponding in 6 (2+) AngularJS(1.x)曾经有一个变量作用域,但我找不到6(2+)中的变量作用域

It seems I was wrong changing the code to following worked, because I placed the *ngFor wrongly at the top to get the view I wanted but not semantically correct one (and I assumed the AngularJS 1.x case of variable scopes): 似乎我将代码更改为以下代码是错误的,因为我错误地将* ngFor放在顶部,以获取所需的视图,但在语义上不正确(我假定变量范围的AngularJS 1.x情况):

<div class="ui styled accordion" style="padding-left: 10px">
  <div class="title" (click)="click($event)" [ngClass]="{'active': active}">
    <i class="dropdown icon"></i>
    {{ node.name }}
  </div>
  <div class="content" [ngClass]="{'active': active}">
    <span *ngFor="let node of node.children">
      <app-og-span [node]="node"></app-og-span>
    </span>
  </div>
</div>

JS: JS:

@Component({
  selector: 'app-og-span',
  templateUrl: './og-span.component.html',
  styleUrls: ['./og-span.component.css']
})
export class OgSpanComponent {
  @Input() node;
  private active = true;

  click($event) {
    this.active = !this.active;
    $event.stopPropagation();
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM