【问题标题】:Hide portion of component in home component Angular在家庭组件Angular中隐藏组件的一部分
【发布时间】:2022-06-15 05:49:57
【问题描述】:

这是我在主页中调用的手风琴组件的 ts 文件。但是,我想使用参数 showOnHome 仅显示布尔值为 true 的参数。我该怎么做?

 @Component({
  selector: "app-faq",
  templateUrl: "./faq.component.html",
  styleUrls: ["./faq.component.css"],
})
export class FaqComponent implements OnInit {
 
  data = [
    {
      id: 1,
      question: "question1",
      answer: [
        "answer here",
      ],
      bullet: false,
      showOnHome: true,
    },
    {
      id: 2,
      question: "question2",
      answer: [
        "answer2",
      ],
      bullet: false,
      showOnHome: false,
    },]```


Called On Home Page as:
``` <app-faq></app-faq> ```

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    您可以通过执行以下操作在显示项目列表之前过滤它们:

    const filteredData = data.filter(d => d.showOnHome);
    

    【讨论】:

      【解决方案2】:

      您可以为您的组件创建一个名为 showHome 的参数并在内部为他过滤。

      export class FaqComponent {
        @Input() showHome: boolean = false;
        filteredData;
      
        data = [
          {
            id: 1,
            question: "question1",
            answer: ["answer here"],
            bullet: false,
            showOnHome: true
          },
          {
            id: 2,
            question: "question2",
            answer: ["answer2"],
            bullet: false,
            showOnHome: false
          }
        ];
      
        ngOnInit() {
          console.log(this.showHome);
          if (this.showHome) {
            this.filteredData = this.data.filter((question) => question.showOnHome);
          } else {
            this.filteredData = this.data;
          }
        }
      }
      
      

      在主页上调用为:

      <app-faq [showHome]="true"></app-faq> 
      

      编辑:创建一个简单的示例,它是如何工作的。 https://codesandbox.io/s/focused-nash-dlgsl?file=/src/app/app.component.html

      【讨论】:

      • 嗨!谢谢,但它似乎没有帮助......它仍然显示相同的页面。只是为了清楚起见,这里我尝试在我的主页中使用 FaqComponent,条件是 showOnHome 为真。如果您能进一步帮助我,我将不胜感激
      • @AditiTripathi 我编辑了原帖,并添加了一个简单的例子来演示完整的实现。希望它可以帮助你。如果问题已解决,请将我的答案标记为正确。 ;)
      • 非常感谢您的帮助!!
      猜你喜欢
      • 2019-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多