标签: angular-ng-if

ng如果不工作

我一直在收到错误

无法绑定到'ngIfElse',因为它不是'ul'的已知属性.

我肯定在某个地方,我犯了一个愚蠢的错误但找不到它.

<ul *ngIf="!finished; else elseBlock" id='time'>
  <li id='hour' class="chart"  data-percent="100"><span>{{hour}} </span></li>
  <li id='min' class="chart" data-percent="100"><span>{{minute}}</span></li>
  <li id='second' class="chart" data-percent="100"><span>{{second}}</span></li>
</ul>
<ng-template  #elseBlock>  <h4 id='time'>DONE</h4> </ng-template>
Run Code Online (Sandbox Code Playgroud)

angular-ng-if angular2-directives angular

6
推荐指数
2
解决办法
3455
查看次数

ngIf - 否则对两个ngIf条件

请考虑以下代码示例:

<div *ngIf="condition; else elseBlock">
    <!-- markup here --> 
</div>
<ng-template #elseBlock>
     <div>
         <!-- additional markup here -->
     </div> 
</ng-template>
Run Code Online (Sandbox Code Playgroud)

我可以实现相同功能的另一种方法是:

<div *ngIf="condition">
    <!-- markup here --> 
</div>
<div *ngIf="!condition">
     <!-- additional markup here -->
</div>
Run Code Online (Sandbox Code Playgroud)

我想知道应该使用这两种方式中的哪种方式的具体原因以及原因?

angular-ng-if angular2-template angular

6
推荐指数
1
解决办法
2194
查看次数

如何在 ngIf 中使用相同的模板

我有很多条件来显示相同​​的模板。例如:

<template [ngIf]="item.url.indexOf('http') == -1">
  <a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >             
    <span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
  </a> 
  <p>Hello, World!</p>
</template>

<template [ngIf]="item.url.indexOf('http') == 0">
  <a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >             
    <span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
  </a> 
  <p>Hello, World!</p>
</template>

<template [ngIf]="item.url.indexOf('http') == 1">
  <a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >             
    <span class="media-body media-middle" *ngIf="isUserLoggedIn == true">{{item.name}}</span>
  </a> 
  <p>Hello, World!</p>
</template>

<template [ngIf]="item.url.indexOf('http') == 2">
  <a class="ripple-effect" [routerLink]="[item.url]" *ngIf="isUserLoggedIn == true" >             
    <span class="media-body media-middle" *ngIf="isUserLoggedIn == …
Run Code Online (Sandbox Code Playgroud)

html javascript typescript angular-ng-if angular

6
推荐指数
1
解决办法
926
查看次数

使用ng-if开始隐藏元素(防止在页面加载期间显示)

当我想创建隐藏的东西时ng-show你可以添加,class="ng-hide"所以css会预先隐藏元素.这样,当页面仍在加载时,不会显示元素

我想做同样的事情,ng-if但我不知道该怎么做

angularjs ng-show angular-ng-if angular-directive

5
推荐指数
1
解决办法
2636
查看次数

ng-if 导致无限循环

首先简要描述我想要做什么:我想迭代数组的元素并显示其内容。根据我从数组内的元素获得的信息,我想显示或不显示按钮。

但这似乎行不通。我们看一下代码:

<div class="padding20" ui-view="create-event-form" ng-controller="detailedViewController" ng-init="loadDetails()">
 <button type="submit" class="button success" ng-if="getParticipationStatus(eventDetails._id)">Participate on this event!</button>
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我通过 ng-if 访问我的DetailedViewController 中的方法。详细视图控制器包含以下方法:

$scope.getParticipationStatus = function( eventId ) {
    var userName = cookieUserManagement.getUsername();
    userClient.getUserEvents( eventId, userName, function ( success, hostingList ) {
        var i = 0;
        var participationStatus;
        if ( success ) {
            hostingList = hostingList.ownEvents;
            if (hostingList.length > 0) {
                for (i = 0; i < hostingList.length; i++) {
                    if (hostingList[i]._id == eventId) {
                        participationStatus = true;
                    } else {
                        participationStatus =  false; …
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angular-ng-if

5
推荐指数
1
解决办法
3463
查看次数

在 Angular 2 中的数字范围上使用 ngSwitch

我最近开始学习Angular2。

我想知道是否有可能使用 ngSwitch 或 ngIf angular2

针对特定数字范围的指令?

假设我想根据范围更新某些文本的颜色。

<25 = Mark the text in red
>25 && <75 = Mark the text in orange
>75 = Mark the text in green
Run Code Online (Sandbox Code Playgroud)

如何使用 Angular2 ngIf/ngSwitch 指令实现相同的效果。

有没有办法写这样的东西?

<div [ngIf]="item.status < 25">
<h2 class="text-red">{{item.status}}</h2>
</div>
<div [ngIf]="item.status > 25 && item.status < 75">
<h2 class="headline text-orange">{{item.status}}</h2>
</div>
<div [ngIf]="item.status > 75">
<h2 class="headline text-green">{{item.status}}</h2>
</div>
Run Code Online (Sandbox Code Playgroud)

或者任何与使用 ngSwitch、ngSwitchWhen 语句有关的内容。

ng-switch angular-ng-if angular

5
推荐指数
1
解决办法
6058
查看次数

*ngIf 指令中的分号与 else 块 - Angular

最近我在 Angular 中使用了一个带有 else 块*ngIf指令并且想知道为什么在Angular 文档中的示例使用分号 ( <div *ngIf="condition; else elseBlock">Content to render when condition is true.</div>) ,因为这似乎也可以在没有分号的情况下工作。

不使用分号有什么副作用吗?或者这只是一种更简洁的编码方式?

html angular-ng-if angular

5
推荐指数
1
解决办法
506
查看次数

为什么 *NgIf 不能使用布尔值来处理 Angular 9

我正在尝试在布尔值上使用 ngif 显示元素,如下所示:

<li *ngIf="!include == true">
    <span class="badge badge-pill badge-danger">
        <i [ngClass]="hearthClass"></i>
    </span>
</li>
Run Code Online (Sandbox Code Playgroud)

我的 include var il 初始化为 false 并像这样切换状态:

this.localStorageService.getCurrentUser().then((res) => {
      this.userProfile = res;
       if(this.song.likes.includes(this.userProfile._id)){
         this.include = true
       }
      this.classHeart();
    });
Run Code Online (Sandbox Code Playgroud)

谢谢大家 !

angular-ng-if angular angular9

5
推荐指数
1
解决办法
1万
查看次数

为什么即使 *ngIf 设置为 true,@ViewChild 仍然未定义

我遇到了以下问题,该问题已由{static: false}中的属性修复@ViewChild。\n此 stackoverflow Q/A 帮助解决了How should I use the new static option for @ViewChild in Angular 8?

\n

我想更好地理解这个场景以及如何static改变结果,以及变化检测如何影响这个场景。我已经阅读了角度文档中有关更改检测的一些内容,并发现极其缺乏。

\n

我想出了一个 stackblitz 来说明一些我不明白的事情。Stackblitz 角度示例

\n

单击toggle按钮两次时,我在命令行上收到以下内容:

\n
> undefined undefined\n> undefined undefined\n> undefined ElementRef\xc2\xa0{nativeElement: div}\n> undefined ElementRef\xc2\xa0{nativeElement: div}\n\n
Run Code Online (Sandbox Code Playgroud)\n

不过我期望:

\n
> undefined undefined\n> undefined ElementRef\xc2\xa0{nativeElement: div}\n> ElementRef\xc2\xa0{nativeElement: div} ElementRef\xc2\xa0{nativeElement: div}\n> ElementRef\xc2\xa0{nativeElement: div} ElementRef\xc2\xa0{nativeElement: div}\n\n
Run Code Online (Sandbox Code Playgroud)\n

这是代码的逻辑——(请参阅 stackblitz 中的完整代码)

\n
> undefined undefined\n> undefined undefined\n> undefined ElementRef\xc2\xa0{nativeElement: div}\n> undefined …
Run Code Online (Sandbox Code Playgroud)

angular-ng-if viewchild angular

5
推荐指数
1
解决办法
2461
查看次数

像 *ngIf 一样使用 'as'

使用 an 时*ngIf,您可以执行类似的操作*ngIf = (value$ | async).property as prop ,然后在整个代码中使用,而无需每次都prop重复 long 。(value$ | async).property 然而,这仅在 是真值时才有效(value$ | async).property- 例如,如果它为零,则无效。

as我的问题是 -如果值是假的,我怎样才能仍然获得好处但仍然让元素显示?as或者更好的是,有没有办法在 an 之外使用*ngIf

angular-ng-if angular

5
推荐指数
1
解决办法
1998
查看次数