【问题标题】:*ngIf not hiding/showing component as expected*ngIf 没有按预期隐藏/显示组件
【发布时间】:2020-04-06 23:43:19
【问题描述】:

我从一个组件调用这个modal.service.ts

export class ModalService {
  serviceText: boolean = true;

  toggleServices() {
    this.serviceText = !this.serviceText;
    console.log('test', this.serviceText);
  }
}

注入到options.component.ts下面...

import { Component, OnInit, Input } from '@angular/core';
import { ModalService } from '../modal.service';

@Component({
  selector: 'app-options',
  templateUrl: './options.component.html',
  styleUrls: ['./options.component.css'],
  providers: [ModalService]
})
export class OptionsComponent implements OnInit {
  serviceText: boolean = false;
  constructor(private modalService: ModalService) { }

  ngOnInit() {
  }

  services() {
    this.modalService.toggleServices();
  }
}

options 组件的 HTML 如下,并且有一个 click 监听器...

    <div class="tooth" >
      <img src="../../assets/tooth-brush.png" alt="tooth-brush" (click)='services()' style='cursor: pointer;'>
      <h5 (click)='services()' style='cursor: pointer;'>Services</h5>
      <app-services *ngIf="serviceText"></app-services>
    </div>

options.component.ts 中的services() 被调用,进而调用modal.service.ts 中的toggleServices()

控制台会记录“测试”以及 True 或 False,这是预期的行为。

但是,

options.component.html 中的 &lt;app-services *ngIf="serviceText"&gt;&lt;/app-services&gt; 没有像我预期的那样在显示或隐藏之间切换。

谁能明白为什么这不起作用?

提前致谢。

【问题讨论】:

  • serviceText 在服务和组件中都定义,选择一个或让组件在模板中指向服务或创建属性。
  • 请记住,在两个不同的类中有两个名为 serviceText 的字段就像有两个人叫 Steve,一个在纽约,一个在西雅图。仅仅因为纽约史蒂夫把四分之一放在他的口袋里并不意味着西雅图史蒂夫现在把它放在口袋里。事实上,它的意思正好相反。

标签: angular angular8 angular-services angular-ng-if


【解决方案1】:

您需要在组件中使用 this.modalService.serviceText 初始化 serviceText,这可能是您忘记做的事情。

此外,由于这是您正在更新的服务中的一个布尔值,它是原始的,因此它将按值传递。

因此,每次在服务上切换 serviceText 时,您都必须在服务中使用 serviceText 重新分配组件中的 serviceText

像这样在你的组件中这样做:

import { Component, OnInit, Input } from '@angular/core';
import { ModalService } from '../modal.service';

@Component({
  selector: 'app-options',
  templateUrl: './options.component.html',
  styleUrls: ['./options.component.css'],
  providers: [ModalService]
})
export class OptionsComponent implements OnInit {
  serviceText: boolean = false;
  constructor(private modalService: ModalService) { }

  ngOnInit() {
    this.serviceText = this.modalService.serviceText;
  }

  services() {
    this.modalService.toggleServices();
    this.serviceText = this.modalService.serviceText;
  }
}

这一切都应该按预期工作。


这里有一个Sample Demo 供您参考。

【讨论】:

  • 不,我只是在我的项目中使用了这段代码,它不起作用。
  • @JamesRossCodes 奇怪。我附上了一个示例演示,我可以看到它在那里工作。 ??‍♂️
  • 只是为了确认 - 我需要做的唯一更改是在 options.component,ts 中?好的,你是对的,现在显示的是模态。正确答案。
  • 是的。这是正确的。这一切都在 StackBlitz 中。 ? 如果单击img 按钮,它将切换hello 组件的可见性。
  • 我认为这种方法还不足以被接受,因为 ModalService 中的 toggleServices() 方法可以花一些时间来执行切换(如果它进行异步调用或在切换之前等待一段时间) state) 因此组件内部的 serviceText 无法反映 ModalService 内部的真实状态。
【解决方案2】:

serviceText 在服务和组件中都定义,选择一个或让组件在模板中指向服务或创建属性。

export class OptionsComponent implements OnInit {

  get serviceText(): boolean { return this.modalService.serviceText; }

  /* rest of code unchanged... */

【讨论】:

  • 我们应该尽量避免setter和getter。我已经写了一个关于同一 here 的答案,其中说明了它的原因。
  • 您可能还想在 Twitter 上查看this thread 的原因。
【解决方案3】:

您需要使用 modalService.serviceText 而不是 serviceText 因为 ModaService 只是更新其内部变量

ts

constructor(public modalService: ModalService) { }

html

<app-services *ngIf="modalService.serviceText"></app-services>

【讨论】:

猜你喜欢
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 1970-01-01
相关资源
最近更新 更多