Javascript *ngIf不像其他*ngIf';s

Javascript *ngIf不像其他*ngIf';s,javascript,html,angular,meteor,Javascript,Html,Angular,Meteor,我的*ngIf在一个特定变量isAdmin上遇到了一个奇怪的问题(这应该允许我在userList中显示用户列表)。我不知道为什么它的行为不同于同一组件中的所有其他*ngIf语句 下面是我的组件js代码片段。如果用户是管理员,isAdmin将从false切换为true _initAutorun(): void { this.autorunComputation = Tracker.autorun(() => { this.zone.run(() => { this.use

我的*ngIf在一个特定变量isAdmin上遇到了一个奇怪的问题(这应该允许我在userList中显示用户列表)。我不知道为什么它的行为不同于同一组件中的所有其他*ngIf语句

下面是我的组件js代码片段。如果用户是管理员,isAdmin将从false切换为true

_initAutorun(): void {
 this.autorunComputation = Tracker.autorun(() => {
  this.zone.run(() => {
    this.usersList = Meteor.users.find({}).fetch(); //update the users list automatically
    this.currentUser = Meteor.user();  //update the current user automatically
    this.isLoggingIn = Meteor.loggingIn();
    this.isLoggedIn = !!Meteor.user();
    this.checkAndSub();
  });
 });
}

checkAndSub(){
 if(this.isLoggingIn){
  Meteor.call('checkAdmin', function(error, result) {
    if (error) {
      this.errors.push(error.reason || "call from server has an error");
    }
    this.isAdmin = result;
    console.log(this.isAdmin);
    if(this.isAdmin){
      Meteor.subscribe("userList");
    }
    else console.log("User is not admin");
  });
 }
}
下面是相应的HTML

<span *ngIf="viewDetails">
  <div class="pizza_details" id="pizza_details" [ngClass]="{'show' : viewDetails, 'hide' : !viewDetails}">
    <div class="row">
      <!--PIZZA DESCRIPTION PANEL-->
      <div class="pizza_description col-lg-4 col-md-4 col-sm-12 col-xs-12">
        <div class="panel" id="description_panel">
          <div class="panel-body">


            <div>
            {{error}}{{message}}
            <span *ngIf="isAdmin"> 
            <p><h2>User List</h2></p>
            <ul>
            <li *ngFor="let iterate of usersList">
            _id: {{iterate._id}}
            <p>Emails: {{iterate.emails}}</p>
            <p>username: {{iterate.username}}</p>
            <p>isadmin: {{iterate.isAdmin}}</p>
            </li></ul>
            </span>
            </div>
            <h1>Description: </h1>
            {{currentPizza.description}}
            <p><button type="button" class="btn active" role="button" (click)="toggle()">Toggle</button></p>


          </div>
        </div>
      </div><!--&&&&pizza description collapes&&&&-->
      <!--STARTING THE "SECOND PANEL" FOR PICTURES, STYLE, VOTES, AND MODS-->
      <div class="col-lg-8 col-md-8 col-sm-12 col-xs-12">
        <div class="row">
          <div class="pizza_picture col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <div class="panel" id="picture_panel">
              <div class="panel-body" style="padding:0px;">
                <img src="{{currentPizza.imageUrl}}" class="img-rounded" style="max-height: 400px; width:100%;">
              </div>
            </div>
          </div><!--&&&&pizza picture collapse&&&&-->
        </div>
        <div class="row">
          <div class="pizza_style col-lg-6 col-md-6 col-sm-12 col-xs-12">
            <div class="panel" id="style_panel">
              <div class="panel-body">
                <h4>Style:</h4>{{currentPizza.style}}
                <h4>Location:</h4>
                <span *ngIf="currentPizza.location.state">
                {{currentPizza.location.state}} , 
                </span>
                {{currentPizza.location.country}}
                <h4>Brand: </h4>{{currentPizza.brand}}
              </div>
            </div>
          </div><!--&&&&pizza style collapse&&&&-->
          <div class="pizza_votes col-lg-6 col-md-6 col-sm-12 col-xs-12">
            <div class="panel" id="vote_panel">
              <div class="panel-body">
                <h3>Pizza Spectrum Score: </h3>
                  {{currentPizza.votes}}
              </div>
            </div>
          </div><!--&&&&pizza votes collapse&&&&-->
        </div>
        <div class="row">
          <div class="pizza_users col-lg-12 col-md-12 col-sm-12 col-xs-12">
            <div class="panel" id="user_panel">
              <div class="panel-body">
                <h3>SubmittedBy: </h3>
                {{submittedBy(currentPizza._id)}}
                <ul><li *ngFor="let i of currentPizza.userUpvotes">{{i}}
                </li>
                </ul>
                <p><button type="button" id="exit_details_btn" class="btn active" role="button" (click)="toggleDetails()">Exit Details</button>

                <button type="button" id="upvote_btn" class="btn active" role="button" (click)="upVote(currentPizza._id)">Upvote</button>
                <button type="button" id="downvote_btn" class="btn active" role="button" (click)="downVote(currentPizza._id)">Downvote</button></p>

                <button type="button" id="downvote_btn" class="btn active" role="button" (click)="cc()">publish all users</button>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
  </span>

{{error}}{{message}
用户列表

    _id:{{iterate.\u id} 电子邮件:{{iterate.Emails}

    用户名:{{iterate.username}

    isadmin:{{iterate.isadmin}}

说明: {{currentPizza.description} 拨动

样式:{{currentPizza.Style} 地点: {{currentPizza.location.state}, {{currentPizza.location.country} 品牌:{{currentPizza.Brand} 比萨饼谱分数: {{currentPizza.voces} 提交人: {{submittedBy(currentPizza.\u id)}
    {{i}
出口详细信息 向上投票 否决票

发布所有用户
我知道isAdmin是真的,但什么都没有出现。如果我创建一个单独的按钮来切换它,则会显示我的用户列表。为什么加载页面时它不能正确显示


谢谢

尝试将span*ngIf更改为div。我的简单检查可能是将span中的内容临时更改为静态文本,例如:hello。那你能看《你好》吗?如果为true,则是标记问题

尝试将span*ngIf更改为div。我简单的检查可能是将span中的内容临时更改为静态文本,例如:hello。那你能看《你好》吗?如果为true,则是标记问题

ngZOne.run
不会将组件标记为进行更改检测。它所做的只是在角度错误处理程序中执行回调函数

您需要注入
changedetechef
并调用
markForCheck

public constructor(private cdr: ChangeDetectorRef, private zone: NgZone) {
}
然后在其他地方:

_initAutorun(): void {
    this.autorunComputation = Tracker.autorun(() => {
         this.zone.run(() => {
             //.....
             this.checkAndSub();
             this.cdr.markForCheck(); // <--- mark component dirty
         });
    });
}
\u initAutorun():void{
this.autorunComputation=Tracker.autorun(()=>{
此.zone.run(()=>{
//.....
this.checkAndSub();

此.cdr.markForCheck();//
ngZOne.run
不会将组件标记为进行更改检测。它所做的只是在角度错误处理程序内执行回调函数

您需要注入
changedetechef
并调用
markForCheck

public constructor(private cdr: ChangeDetectorRef, private zone: NgZone) {
}
然后在其他地方:

_initAutorun(): void {
    this.autorunComputation = Tracker.autorun(() => {
         this.zone.run(() => {
             //.....
             this.checkAndSub();
             this.cdr.markForCheck(); // <--- mark component dirty
         });
    });
}
\u initAutorun():void{
this.autorunComputation=Tracker.autorun(()=>{
此.zone.run(()=>{
//.....
this.checkAndSub();

this.cdr.markForCheck();//在得到结果后是否会触发此日志语句?console.log(this.isAdmin);如果触发,并且没有发生更改检测,则可能需要从“@angular/core”导入{ChangeDetectorRef};在构造函数put构造函数(cdr:ChangeDetectorRef){this.cdr=cdr;}然后日志语句放在哪里“this.cdrdtecttchanges();”是的,日志触发了。我以后有机会会尝试一下,谢谢你是在数组中管理的吗?@Rahul它不是数组你应该(?)在
this.admin
中失去
this
的作用域:
Meteor.call('checkAdmin',函数(错误,结果){
因为您使用的是
函数
而不是胖箭头语法?还是我遗漏了什么:在您得到结果后,DDoS会触发此日志语句?console.log(this.isAdmin);如果是这样,并且没有发生更改检测,您可能希望从“@angular/core”导入{ChangeDetectorRef};在构造函数中放入构造函数(cdr:ChangeDetectorRef){this.cdr=cdr;}然后在日志语句的位置'this.cdrDetectchanges();'是的,日志会触发。我稍后有机会会尝试一下,感谢isAdmin是一个数组?@Rahul它不是一个数组你应该(?)在
this.admin
中失去
this
的作用域:
Meteor.call('checkAdmin',函数(错误、结果){
因为您使用的是
函数
而不是胖箭头语法?或者我遗漏了什么:d此解决方案似乎不起作用。我将在上午尝试使用ChangeDetectorRef类并报告我的发现。此解决方案似乎不起作用。我将在上午尝试使用ChangeDetectorRef类并报告我的发现。