标签: angular-ng-if

AngularJS ng-if指令在删除元素之前即使条件为false也会短暂呈现

在下面的模板中,我希望脚本标记永远不会呈现,并且警报脚本永远不会执行.不过确实如此.

<div ng-if="false">
    <script>alert('should not run')</script>
    Should not appear
</div>
Run Code Online (Sandbox Code Playgroud)

这导致我们在移动设备上出现巨大的性能问题,因为我们在ng-ifs中包含大型DOM和指令结构,期望它们在条件为假时不会呈现.

我也测试了ng-switch哪种行为方式相同.

这是预期的行为吗?有没有办法避免不必要的渲染?

的jsfiddle

javascript angularjs angularjs-directive angular-ng-if

7
推荐指数
1
解决办法
6793
查看次数

Angular2使用*ngIf和焦点指令?

在我的应用程序中,我尝试放置一个按钮,显示/隐藏带有布尔组件属性的输入字段.如果按钮显示输入,则应在输入上设置焦点.但它似乎不起作用.如果我删除input焦点指令工作正常.

我创造了一个显示我的意思的plunker.描述我的"问题"有点困难.

组件中的html

<input *ngIf="filterShow.options"
       [focus]="filterFocus.options"
       [(ngModel)]="filter.options">

<button type="button"
        (click)="setShowFilter('options')">
  focus
</button>
Run Code Online (Sandbox Code Playgroud)

setShowFilter()函数

private setShowFilter(filter: string) {
  this.filterShow[filter] = !this.filterShow[filter];

  /* reset filter */
  this.filter[filter] = "";

  this.filterFocus[filter].emit(true);
}
Run Code Online (Sandbox Code Playgroud)

focus.directive.ts

@Directive({
  selector: '[focus]'
})
export class FocusDirective implements OnInit {

  @Input('focus') focusEvent: EventEmitter<boolean>;

  constructor(private elementRef : ElementRef,
              private renderer   : Renderer   ) { }

  ngOnInit() {
    this.focusEvent.subscribe(event => {
      this.renderer
        .invokeElementMethod(this.elementRef.nativeElement, 'focus', []);
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

focus input angular-ng-if angular

7
推荐指数
1
解决办法
5423
查看次数

在Angular中使用ngIf获取错误

我使用的角度,我想从一个变量,它是JSON形式.I'm能够做到这一点的显示数据*ngfor而且我希望把它检查是否msg.who =="博特"的条件我正在这样做:

<div class="UserWrapper" *ngFor="let msg of msgs" ngIf="msg.who == 'User ">
    <div class=""></div>
        <div class = "speech-bubble1 z-depth-5"><p>{{msg.msg}}</p>
            <p class="timeRight">{{msg.time}}</p>
    </div>
 </div>
Run Code Online (Sandbox Code Playgroud)

我不知道如何使用ng如果另一种方式,但这样做我得到以下错误.

ERROR Error: StaticInjectorError(AppModule)[NgForOf -> TemplateRef]: 
StaticInjectorError(Platform: core)[NgForOf -> TemplateRef]: 
NullInjectorError: No provider for TemplateRef!
Run Code Online (Sandbox Code Playgroud)

我该如何检查给定的条件

angular-ng-if angular

7
推荐指数
1
解决办法
1860
查看次数

如何在AngularJS ng-if语句中使用">"比较器

是否可以在HTML中使用ng-if中的"大于"比较器?问题是">"符号过早地关闭了HTML标记.

恩.这个:<div ng-if="foo>0" class="bar"> (HTML STUFF) </div>

读作: <div ng-if="foo"> (0 class="bar"> HTML STUFF) </div>

我最后通过使用ng-if ="foo!= 0"来解决这个问题,但我可能会使用小于比较器,但我只是好奇,以防我因某种原因绝对不能使用大于符号.或者我是否可能必须将此逻辑移动到其他地方,例如在我的控制器中,而不是在我的视图中?

编辑1 所以看起来似乎比较器本身不是问题,我的代码中还有其他东西正在发生.奇怪的是,当我在比较器之前和之后有空位时,它可以工作,但是没有空格则没有.如果这意味着什么,我也使用棱角1.3.15.

<div class="paginate" ng-if="list.total > 0"> 作品

<div class="paginate" ng-if="list.total>0"> 不起作用

html javascript angularjs angular-ng-if

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

Angular 2无法绑定到'ngif',因为它不是已知属性

我正在试验Angular 2.我有一个有两个组件的工作测试应用程序.这一切都有效,但在我尝试使用时崩溃了*ngif

关于这个问题已经有很多问题,但大多数答案都指向导入"BrowserModule"来解决问题.就我而言,我已经这样做了.还有什么可能导致这个问题?

我的HTML

<table class='table' *ngif="products && products.length">
Run Code Online (Sandbox Code Playgroud)

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppComponent }  from './app.component';
import { CrazyComponent }  from './products/crazy.component';

@NgModule({
      imports: [ BrowserModule ],
      declarations: [ AppComponent, CrazyComponent ],
      bootstrap: [ AppComponent ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)

完整的错误:

无法绑定到'ngif',因为它不是'table'的已知属性.嵌入式模板上的任何指令都未使用的属性绑定ngif.确保属性名称拼写正确,并且所有指令都列在"指令"部分中

typescript angular-ng-if angular

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

当期望(结果).toBe(null)测试失败时,测试冻结.(Angular 2,Jasmine)

我有一个测试,检查部分DOM元素是否已被ngIf删除.当我使用以下方法检查DOM时:fixture.debugElement.query(By.css(".button-area"))resultnullDOM元素或者是DOM元素.

如果resultnull,那么以下测试工作正常.但是,如果测试result包含一个元素,它不会简单地通过测试,它会冻结浏览器.

测试看起来像这样:

var result = fixture.debugElement.query(By.css(".button-area"))
expect(result).toBe(null)
Run Code Online (Sandbox Code Playgroud)

我也尝试过expect(result).toEqual(null),.toBeFalsy()结果相同.

测试DOM元素是否已正确删除的正确方法是什么?

更新 1/23/2017

我发现这个问题是由返回的元素指定的:

fixture.debugElement.query(By.css(".button-area"))

这可能是角2或茉莉的错误.如果我使用document.getElementByClassName("button-area")它不是一个问题,测试工作正常.

dom jasmine angular-ng-if karma-jasmine angular

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

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
查看次数

当变量发生变化时,NgIf不会更新

是的,所以我有一个包含以下模板的标题组件(navbar):

<ng-template [ngIf] = "authService.isAuthenticated()">
  <li>
    <a routerLink="Landing" class="navbar-brand" (click)="Logout()"><span class="xvrfont">Logout</span><i class="fa fa-sign-in" aria-hidden="true"></i></a>
  </li>
  <li>
    <a routerLink="Profile" class="navbar-brand"><span class="xvrfont">{{authService.getUsername()}}</span><i class="fa fa-user-circle" aria-hidden="true"></i></a>
  </li>
</ng-template>
Run Code Online (Sandbox Code Playgroud)

当用户通过身份验证时,导航的这一部分应该是可见的.找出它通过authService检查.

要检查用户是否已通过身份验证,将在每次路由更改时运行以下代码:

checkAuthenticated(){
   if  (localStorage.getItem('token') != null){ this.authenticated = true; }
   else { this.authenticated = false; }
   console.log(this.authenticated); // for Debugging. 
}
Run Code Online (Sandbox Code Playgroud)

NgIf语句调用此方法:

public isAuthenticated(){
     return this.authenticated;
}
Run Code Online (Sandbox Code Playgroud)

根据日志,"证实" 真假之间切换正确,但Ngif不响应的变化不知何故.

标头component.ts看起来像这样:

import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import {AuthService} from "../auth/auth.service";

@Component({
  selector: 'app-header',
  providers: [AuthService],
  templateUrl: './header.component.html',
  styleUrls: ['./header.component.css'],
  encapsulation: ViewEncapsulation.None …
Run Code Online (Sandbox Code Playgroud)

service typescript angular-ng-if angular

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