【问题标题】:Angular 5 - Component with ngIf creating duplicates instead of toggleAngular 5 - 使用 ngIf 创建重复项而不是切换的组件
【发布时间】:2018-04-03 22:18:46
【问题描述】:

我正在尝试制作一个可重用的组件,该组件基本上在单击时从一个图标切换到另一个图标。我尝试过使用和不使用 ng-template、使用 *ngIf 和使用 switch case。

根据评论编辑

这是我的例子:

图标切换组件

@Component({
    selector: 'app-icon-toggle',
    template: `    
        <a [ngClass]="iconStyle" [ngSwitch]="active">
            <i *ngSwitchCase="true" class="fa {{activeIcon}}"></i>
            <i *ngSwitchCase="false" class="fa {{inactiveIcon}}"></i>
        </a>
    `
})
export class IconToggleComponent implements AfterContentChecked {

    @Input() active: boolean;

    @Input() activeIcon: string;
    @Input() inactiveIcon: string;

    @Input() iconStyle: string;

    ngAfterContentChecked(): void {
      console.log('content check', this.active);
    }
}

使用它的组件

@Component({
  selector: 'app-admin-info-toggle',
  template: `
      <template #loading><i></i></template>

      <div style="display: flex; flex-direction: row; align-items: start;">

      <app-icon-toggle [active]="showChangeInfo" 
                       [activeIcon]="'fa-eye'" 
                       [inactiveIcon]="'fa-eye-slash'" 
                       [iconStyle]="'show-change-info'" 
                       (click)="toggleShowChangeInfo()">
      </app-icon-toggle>

      <!-- ORIGINAL SETUP THAT I AM EXTRACTING -->
      <div *ngIf="lockEditing; then editIcon else lockIcon"></div>

      <ng-template #editIcon>
          <a class="change-lock" (click)="toggleEdit()">
              <i class="fa fa-edit"></i>
          </a>
      </ng-template>

      <ng-template #lockIcon>
          <a class="change-lock" (click)="toggleEdit()">
              <i class="fa fa-lock"></i>
          </a>
      </ng-template>
  </div>
`
})
export class AdminInfoToggleComponent implements OnInit {
    lockEditing = true;
    showChangeInfo = true;

    constructor(private sessionService: SessionService) {}

    ngOnInit(): void {
        this.sessionService.getLockEditing()
            .subscribe(isLocked => this.lockEditing = isLocked);
    }

    toggleEdit() {
       this.sessionService.toggleEditingLock(this.lockEditing)
             .subscribe(isLocked => this.lockEditing = isLocked);
    }

    toggleShowChangeInfo() {
        this.showChangeInfo = !this.showChangeInfo;
    }
}

会话服务

@Injectable()
export class SessionService implements OnInit {

      lockEditing = new Subject<boolean>();

      ngOnInit(): void {
          this.lockEditing.next(true);
      }

      getLockEditing() {
        return this.lockEditing;
      }

      toggleEditingLock(isLocked: boolean) {
          this.lockEditing.next(!isLocked);

          this.lockEditing.subscribe(isLocked => 
              console.log('toggleEditingLock [result]', isLocked));

          return this.lockEditing;
      }
}

我想它需要更多细节,因为它只是代码。

Duplicating icons

【问题讨论】:

    标签: angular toggle angular5 angular-ng-if


    【解决方案1】:

    您可以做到这一点的一种方法是提供一个服务,其中包含一个名为“toggled”的变量,并在您的图标切换组件中根据状态将服务中的切换变量设置为 true 或 false。然后在您的组件中使用切换订阅服务中切换的值,然后执行 *ngIf 以指定是否应显示对象。

    例如:

    图标切换组件

    @Component({
        selector: 'app-icon-toggle',
        template: `    
            <a [ngClass]="iconStyle" [ngSwitch]="active" (click)="inputPressed()">
                <i *ngSwitchCase="true" class="fa {{activeIcon}}"></i>
                <i *ngSwitchCase="false" class="fa {{inactiveIcon}}"></i>
            </a>
        `
    })
    export class IconToggleComponent implements AfterContentChecked {
        
        constructor(inputService: InputService) {}
        
        inputPressed() {
         this.inputService.inputValChanged.next(!this.inputVal);
        }
    
        @Input() active: boolean;
    
        @Input() activeIcon: string;
        @Input() inactiveIcon: string;
    
        @Input() iconStyle: string;
    
        ngAfterContentChecked(): void {
          console.log('content check', this.active);
        }
    }

    图标服务

    export service IconService {
    
                inputVal = true;
                inputChanged: Subject<number> = new Subject<number>;
                
                inputValChanged() {
                   this.inputChanged.next(!inputVal);
                   this.inputVal = !this.inputVal;
                }
            }

    要切换的组件

    @Component({
        selector: 'app-icon-toggle',
        template: <div *ngIf="currentInputVal"></div>
    })
    export class ComponentToBeToggeld implements OnIt {
    
        currentInputVal;
        currentValSubscription;
    
        constructor(inputService: InputService) {
          this.currentValSubscription = this.inputValChanged(val => {
             this.currentInputVal = val;  
          });
        }
        
        
        
    }

    【讨论】:

    • 我不在一个可以工作的环境面前,所以这是如何回答这个问题的要点,但有些事情可能有点偏离
    • 我实际上有类似的东西,但它会导致相同的行为。当我有机会时,我会做一个 plunkr,但运气好的话,它会很好用。不知道是我做错了还是是一些不为人知的错误。
    • 无。一切正常(甚至控制台正确的锁定布尔值)。我想知道为什么当我只声明了 2 个可能的状态时它会制作多个副本。似乎dom并没有破坏价值变化的元素。我会在帖子底部添加点击结果的截图。
    • 您可以将 dom 元素设置为具有 style='display: none' 属性吗?
    猜你喜欢
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多