简体   繁体   English

*ngIf Angular 没有绑​​定正确的值

[英]*ngIf Angular not binding the right values

I am using the below code in ngOnInit() to use it in the HTML to bind some content and for some reason unable to identify the issue and not printing anything on the web page!我在ngOnInit()中使用以下代码在 HTML 中使用它来绑定一些内容,但由于某种原因无法识别问题并且没有在网页上打印任何内容! Can someone guide or help, please?有人可以指导或帮助吗?

TypeScript:打字稿:

public ngOnInit() { 

    let one = true;
    let two = false;
    let three = true;
    let four = true;

    let caseA = one && two && three && four;
    let caseB = one && !two && three && four; // should be true
    let caseC = one && two && !three && !four;
    let caseD = one && two && three && !four;
    let caseE = one && !two && !three && !four;
    
    console.log("cases: ", caseA, caseB, caseC, caseD, caseE);

}

HTML: HTML:

<p class="msg" *ngIf="caseA">{{ valueA }}</p>
<p class="msg" *ngIf="caseB">{{ valueB }}</p>
<p class="msg" *ngIf="caseC">{{ valueC }}</p>
<p class="msg" *ngIf="caseD">{{ valueD }}</p>
<p class="msg" *ngIf="caseE">{{ valueE }}</p>

Thank you谢谢

You need to declare public variables in your .ts file, so template can access them您需要在 .ts 文件中声明公共变量,以便模板可以访问它们

export class AppComponent implements OnInit{

 public caseA = true

  public ngOnInit() {

    let one = true;
    let two = false;
    let three = true;
    let four = true;

    let caseA = false // have no impact at ngIf in template
    let caseB = one && !two && three && four; // should be true
    let caseC = one && two && !three && !four;
    let caseD = one && two && three && !four;
    let caseE = one && !two && !three && !four;

    console.log("cases: ", caseA, caseB, caseC, caseD, caseE);

}

html html

<p class="msg" *ngIf="caseA">Hey there</p>

I believe you just haven't declared at the right spot.我相信你只是没有在正确的地方申报。 Try declaring the variables as public in your component like the following :尝试在您的组件中将变量声明为 public,如下所示:

export class YourComponent implements OnInit {
    public caseA: boolean;
    public caseB: boolean;
    public caseC: boolean;
    public caseD: boolean;
    public caseE: boolean;

  ngOnInit(): void {
    const one = true;
    const two = false;
    const three = true;
    const four = true;

    this.caseA = one && two && three && four;
    this.caseB = one && !two && three && four; // should be true
    this.caseC = one && two && !three && !four;
    this.caseD = one && two && three && !four;
    this.caseE = one && !two && !three && !four;

    console.log("cases: ", this.caseA, this.caseB, this.caseC, this.caseD, this.caseE);
  }

You need to declare the variables caseA, caseB, caseC.... as instance variables by declaring them outside the methods but inside the class.您需要通过在方法之外但在类内声明变量 caseA、caseB、caseC.... 作为实例变量来声明它们。

Then, you should use those variables inside the ngOninit method with this keyword.然后,您应该使用this关键字在 ngOninit 方法中使用这些变量。

Since you have used valueA, valueB, valueC.... in HTML file, you need to declare and initialize them in the ts file too.由于您在 HTML 文件中使用了 valueA、valueB、valueC....,因此您也需要在 ts 文件中声明和初始化它们。

You don't need to put public keyword, if you are going to use those instance variables only within the component.如果您打算仅在组件内使用这些实例变量,则不需要放置public关键字。

export class AppComponent implements OnInit  {

caseA: boolean;
caseB: boolean;
caseC: boolean;
caseD: boolean;
caseE: boolean;

valueA = 'a';
valueB = 'b';
valueC = 'c';
valueD = 'd';
valueE = 'e';

public ngOnInit() { 

 let one = true;
 let two = false;
 let three = true;
 let four = true;

 this.caseA = one && two && three && four;
 this.caseB = one && !two && three && four; // should be true
 this.caseC = one && two && !three && !four;
 this.caseD = one && two && three && !four;
 this.caseE = one && !two && !three && !four;

 console.log("cases: ", this.caseA, this.caseB, this.caseC, this.caseD, 
 this.caseE);

 }

}

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

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