简体   繁体   English

Angular 6 +使用ngIf显示隐藏元素不起作用

[英]angular 6 + show hide element with ngIf doesn't work

I'm new to angular. 我是新手。 I have the app.component.html like this: 我有这样的app.component.html:

<app-login *ngIf="!loggedIn"></app-login>
<section *ngIf="loggedIn" style="background:#EBF0F5;">
    <div class="container">       
        <div class="board">                    
            <msw-navbar ></msw-navbar>      
            <div class="tab-content">
                <!-- Nested view  -->
                <ui-view></ui-view>
            </div>
            <!-- End Content Area -->
        </div>
        <pre>{{ formData | json }}</pre>
    </div>
</section>

I want to show the login page and hide after login without any control or something. 我想显示登录页面并在登录后隐藏而不进行任何控制。 I have the login.ts 我有login.ts

export class LoginComponent implements OnInit {
  logginIn: boolean = true;
}

in login click: 在登录中单击:

 login() {
    this.logginIn = true;
}

in app.component.ts 在app.component.ts中

    export class AppComponent implements OnInit {      
    @Input() formData;
    loggedIn: boolean = false;
}

the login.html login.html

  <div class="content">
  <form class="login-form">
      <h3 class="form-title">Login to your account</h3>
      <div class="alert alert-danger display-hide" style="display: none;">
        <button class="close" data-close="alert"></button>
        <span> Enter any username and password. </span>
      </div>
      <div class="form-group">
        <div class="input-icon">
          <i class="fa fa-user"></i>
          <input id="userName" name="userName" 
          class="form-control" required
          [(ngModel)]="user.userName"
          autofocus="autofocus" />
        </div>
      </div>
      <div class="form-group">
        <div class="input-icon">
          <i class="fa fa-lock"></i>
          <input id="password" name="password" 
          class="form-control" required 
          [(ngModel)]="user.password"
          type="password"/>
        </div>
      </div>

      <div class="row">
          <div class="col-xs-12">
            <div class="alert alert-danger" 
            *ngIf="securityObject &&
                   !securityObject.isAuthenticated">
              <p>Invalid User Name/Password.</p>
            </div>
          </div>
        </div>
      <br>
      <div class="text-right">
          <button class="btn-lg btn pull-right btnCol" (click)="login()">
              Login
            </button>  
      </div>
    </form>

What should I add in app.component to get the value from login component true or false? 我应该在app.component中添加什么才能从登录组件中获取值为true或false? how the component communicate with each other? 组件如何相互通信? Thank you 谢谢

In your app.component.html : 在您的app.component.html中:

<app-login *ngIf="!loggedIn" (loggedIn)="isLogged($event)"></app-login>

in your login.component.ts : 在您的login.component.ts中:

@Output() loggedIn = new EventEmitter<boolean>();

login() {
 this.loggedIn.emit(true);
}

in your app.component.ts : 在您的app.component.ts中:

isLogged(logged:boolean) {
  this.loggedIn = logged;
}

I hope this will help 我希望这个能帮上忙

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

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