Angular 5 - 使用ngIf创建重复项而不是切换

时间:2018-04-03 22:18:46

标签: angular toggle angular5 angular-ng-if

我正在尝试创建一个可重用的组件,在单击时基本上从一个图标切换到另一个图标。我尝试使用和不使用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

1 个答案:

答案 0 :(得分:0)

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

例如:

图标切换组件

&#13;
&#13;
export service IconService {

            inputVal = true;
            inputChanged: Subject<number> = new Subject<number>;
            
            inputValChanged() {
               this.inputChanged.next(!inputVal);
               this.inputVal = !this.inputVal;
            }
        }
&#13;
&#13;
&#13;

图标服务

&#13;
&#13;
@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;  
      });
    }
    
    
    
}
&#13;
&#13;
&#13;

要切换的组件

&#13;
&#13;
host name
&#13;
&#13;
&#13;