迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > Angular >

在 Angular 上使用 ngIf Else

作者:迹忆客 最近更新:2023/04/14 浏览次数:

当我们作为开发人员构建 Web 应用程序时,我们需要设置 Web 应用程序以动态调整页面; *ngIf 语句变得很方便。 *ngIf 用于操作呈现在页面上的 HTTP 元素。

它在我们告诉网络应用程序在一些参数到位后执行某个功能的条件下工作。而当这些参数没有到位时,它应该执行另一种代替。

我们将只使用*ngIf 来执行一些功能。然后我们将把 *ngIfelse 语句结合起来,看看有什么可能。最后,我们将使用 *ngIf else then 组合来操作 HTTP 元素。


在 Angular 中使用*ngIf 作为一个独立的函数

所以在这里,我们将使用*ngIf 语句以及 truefalse 条件。

我们将在 app.component.html 中完成所有工作。

<h1>*ngIf in Angular</h1>
<h2 *ngIf="true">
  Hi, Youtube
</h2>

*ngIf 条件设置为真时,网页显示 Hi, Youtube。一旦更改为 false,Hi, Youtube 将从网页中消失。

我们也可以像这样通过 app.component.ts 传递函数:

import { Component } from "@angular/core";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent {
  title = "CodeSandbox";
  displayMe=true;
}

然后我们将*ngIf 值更改为 displayMe,如下所示:

<h1>*ngIf in ANgular</h1>
<h2 *ngIf="displayMe">
  Hi, Youtube
</h2>

就像第一个示例一样,基本规则的工作方式相同。


*ngIfelse 一起使用

*ngIf 条件与 else 语句结合使用时,它可以帮助我们决定在任何时间点我们希望在网页上显示什么内容。

这种方法要求我们在 ng-template 标签内嵌套我们想要出现在 Hi, Youtube 中的另一个语句。

app.component.html 将如下所示:

<h1>*ngIf in ANgular</h1>
<h2 *ngIf="displayMe else elseBlock">
  Hi, Youtube
</h2>
<ng-template #elseBlock>
<h2>
    Hi, Twitter
    </h2>
</ng-template>

你会注意到我们在*ngIf 条件旁边使用了 else 语句,这使我们能够更好地控制网页的内容。

所以在 app.component.ts 中,当 displayMe 为真值时,Hi, Youtube 将显示。如果它是假的,嗨,Twitter 将显示。


在 Angular 中使用 *ngIfelsethen 语句

在前面的示例中,我们被介绍了 ng-template,它与 else 语句一起使用。当与 *ngIfelse 一起使用时,then 语句允许在 ng-template 中操作内容。

对于想要保持结构化且排列良好的代码文件的编码人员来说,这是一个受欢迎的解决方案。

我们的 app.component.html 将被配置为:

<h1>*ngIf in ANgular</h1>
<h2 *ngIf="displayMe; then ifBlock else elseBlock">
  Hi, Youtube
</h2>
<ng-template #ifBlock>
<h2>
    Hi, Google
    </h2>
</ng-template>
<ng-template #elseBlock>
<h2>
    Hi, Twitter
    </h2>
</ng-template>

所以在 app.component.ts 中,当 displayMe 为真值时,会显示 Hi, Google,当它是假值时,会显示 Hi, Twitter

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

用 jQuery 检查复选框是否被选中

发布时间:2024/03/24 浏览次数:102 分类:JavaScript

在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便