简体   繁体   English

Angular4 中的 *ngIf 问题

[英]Issue with *ngIf in Angular4

*ngIf is not working properly in angular4. *ngIf 在 angular4 中无法正常工作。

Step 1: While loading the application displays processing image and next line 'true'.第 1 步:加载应用程序时显示正在处理的图像和下一行“true”。
Step 2: once the page is loaded i am setting the busy boolean var as false in my component.ts.第 2 步:加载页面后,我将在 component.ts 中将 busy boolean var 设置为 false。 once the page is loaded i am seeing processing image , then next line it displays 'true' and then next line it displays 'false'.页面加载后,我看到正在处理的图像,然后下一行显示“真”,然后下一行显示“假”。

In the step 2 it should display only false.在第 2 步中,它应该只显示 false。 It should not display processing image and then 'true'.它不应该显示处理图像,然后显示“true”。

Below is the html code.下面是html代码。 Can you please help me to fix this issue?你能帮我解决这个问题吗?

     <div *ngIf="busy">
            <h4 align="center"><img src="assets/images/Processing.gif"></h4>
      {{busy}}
    </div>
    <div *ngIf="!busy">
      {{busy}}
    <!-- other  components -->
    </div>

component.ts(pasted only few lines of code)

import {Component, OnInit} from '@angular/core';
import {maptargetService} from '../shared';
import {maptarget} from '../shared/maptarget/maptarget.model';
import {SelectItem} from 'primeng/primeng';
import {AdalService}from './../services/adal.service';
import { Router } from '@angular/router';
import { ActivatedRoute, Params} from '@angular/router';


@Component({
    selector: 'app-maptarget',
    templateUrl: './maptarget.component.html',
    styleUrls: ['./maptarget.component.css'],
    providers: [maptargetService]
})

export class maptargetComponent  implements OnInit {
    busy: boolean;
    isLoggedIn: any;


   constructor(private maptargetService: maptargetService, private adalService: AdalService, private router: Router, private route: ActivatedRoute) { }

   ngOnInit() {
       this.isLoggedIn = this.adalService.isAuthenticated;
       this.busy = true;
       this.isEnable = false;
       this.resetCardValues();
       // if(this.owner == null){
            this.owner = 'Summary';
       // }
       this.isOrgSelected = false;
     
       this.filteredOrg = '0';
       this.filteredBay = 'noBay';
       this.metricsTitle = 'orgTitle';
         this.getmaptargetDetails(this.filteredOrg, this.owner, this.filteredBay);
          this.getDataTableList(this.owner);
          this.busy = false;
       }
   }

I guess your busy is a string instead of a boolean,我猜你的 busy 是一个字符串而不是一个布尔值,

Try试试

<div *ngIf="busy ==='true'">
        <h4 align="center"><img src="assets/images/Processing.gif"></h4>
  {{busy}}
</div>
<div *ngIf="busy==='false'">
  {{busy}}
<!-- other  components -->
</div>

busy boolean var as false繁忙的布尔变量为假

your variable should not be var a variable.你的变量不应该是var a 变量。 it should be something like this.busy = false它应该是这样的this.busy = false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM