如何在angular4

时间:2018-02-21 05:03:58

标签: javascript angular typescript eventemitter

主页和用户页面的内容显示在主页本身上。 在标题部分,我有来自home.html的SignIn和SignUp表单。 我在用户页面中有另一个Signup form,其中我有3个按钮,signUp with facebooksignUp with googleSignUp,其中包含姓名,电子邮件和密码。 当我使用signUp with facebooksignUp with google注册时,主页的标记从SignIn and SignUp更改为User and SignOut,并且用户页面的SignUp形式将被隐藏。

但是,当我使用姓名,电子邮件和密码signUp时,这种情况不起作用。

我也使用了发射器发射器,但这不仅仅适用于SignUp。任何人都可以提供帮助。

Home.html中:

<ul>
<li class="signUp" *ngIf = "loggedIn">{{userName}}</li>
<li class="signIn" (click)="signOut()" *ngIf = "loggedIn">Sign Out</li>
<li class="signUp" (click)="openSignUp(user)"  *ngIf = "!loggedIn">Sign Up</li>
<li class="signIn" (click)="openSignIn(logedin)"  *ngIf = "!loggedIn">Sign In</li>
</ul> 

User.html:

<div class="favourate" *ngIf="!loggedIn">
    <h1>User</h1>
    <hr />
    <div class="backImage">
      <form name="signUpForm" class="signUpForm" #signUpForm="ngForm" novalidate>
        <div class="form-group">
          <h3>Sign Up</h3>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>perm_identity</mat-icon>
            <input matInput type="text" placeholder="Name" name="name" [(ngModel)]="name" #Name="ngModel" required>
          </mat-form-field>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>email</mat-icon>
            <input matInput type="email" placeholder="Email" name="email" [(ngModel)]="user.email" #Email="ngModel" pattern="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" required>
          </mat-form-field>
        </div>
        <div class="form-group input">
          <mat-form-field> 
            <mat-icon>lock</mat-icon>
            <input matInput type="password" placeholder="Password" name="password" [(ngModel)]="user.password" #Password="ngModel" required>
          </mat-form-field>
        </div>          
      </form>
    </div>
  </div>

User.ts:

this.ApiService
      .checklogin()
      .subscribe(
        user  => {
          this.loggedIn = localStorage.getItem("loggedin");
          this.user_id = user.data[0].user_id;
          this.userName = user.data[0].name;
        }, error => {
          console.log(error);
        });
      newUser(user,_id) {
        this.isLoadingSignUp = true;
        user.role_id = this._id 
        var data = {
          "user":{
          email:user.email,
          password:user.password,
          active:user.active,
          role_id:this._id,
          name:user.name
           }     
        }
        this.ApiService
            .signUp(data)
            .subscribe(
              signUser => {
                  this.userName = user.name;
                  localStorage.setItem('loggedin', 'true');
                  this.loggedIn = true;
                  this.isLoadingSignUp = false;
                  this.router.navigate(['/home']);
                  this.toasterService.pop('success', 'SignUp Successfully');
                  this.ApiService.getUserData(user.data);
               }, error => {
                  this.isLoadingSignUp = false;
                  this.ApiService.getUserData(error);
                  if(error.data && error.data.length > 0) {
                    this.toasterService.pop('error', error.data);
                  } else {
                    this.toasterService.pop('error', 'Something went wrong!');
                  }
               })
      }

2 个答案:

答案 0 :(得分:1)

根据您的要求和项目流程结构,您可以从浏览器获取帮助。 这意味着每次用户使用facebook或google登录或注册时。您可以在浏览器本地存储中存储唯一值。并可以检索或使用它来检查用户登录的位置。该值可以简单地用于ngIf条件。喜欢何时展示以及何时不展示。

我希望你理解我的意思。

答案 1 :(得分:1)

您可以使用Observablesubject在不同的组件之间进行通信。

您应该使用服务来执行此操作。

创建一个名为SignUpService的服务,该服务会创建一个新的subject来表示登录

import { Injectable } from '@angular/core';
import { Subject }    from 'rxjs/Subject';

@Injectable()
export class SignUpService {
  private signInSource = new Subject<string>();

  signinDone$ = this.signInSource.asObservable();

  signIn() {
    this.signInSource.next();
  }
}

subject

中拨打signIn component
import { SignInService }     from './signin.service';

@Component({
})
export class SignInComponent {
    this.SignInService.signIn();
}

Header component

中接收该事件
import { SignInService }  from './signin.service';
import { Subscription }   from 'rxjs/Subscription';

@Component({
})
export class HeaderComponent implements OnDestroy {
  subscription: Subscription;

  constructor(private signInService: SignInService) {
    this.subscription = signInService.signinDone$.subscribe(
      signin => {
        alert('came here')
    });
  }

  ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}