【发布时间】:2018-06-15 05:20:35
【问题描述】:
AuthComponent
import { TopnavComponent } from './../topnav/topnav.component';
import { Component, OnInit,ViewContainerRef } from '@angular/core';
import {AuthService} from "./auth.service";
import { NgForm } from '@angular/forms';
import {Router} from "@angular/router";
import {Profile} from "./auth.model";
import {Http,Request,Response,Headers, RequestOptions} from "@angular/http";
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
@Component({
selector: 'app-auth',
templateUrl: './auth.component.html',
styleUrls: ['./auth.component.css'],
providers:[TopnavComponent]
})
export class AuthComponent implements OnInit {
constructor(private authUser: AuthService, private router: Router,public topnav:TopnavComponent) {};
callMethod() {
document.cookie = 'uid='+ this.isAuthSuccess.profile[0].guProfileId;
document.cookie = 'isInstructor='+ this.isAuthSuccess.profile[0].isInstructor;
document.cookie = 'isStudent='+ this.isAuthSuccess.profile[0].isStudent;
this.topnav.changeState();
this.router.navigateByUrl('/home');
});
}
}
顶部导航组件
import { Component, OnInit } from '@angular/core';
import {Http,Request,Response,Headers, RequestOptions} from "@angular/http";
import { HttpClient, HttpHeaders } from '@angular/common/http';
import {Router, ActivatedRoute} from "@angular/router";
import { Observable } from 'rxjs';
import {AuthService} from "../auth/auth.service"
@Component({
selector: 'app-topnav',
templateUrl: './topnav.component.html',
styleUrls: ['./topnav.component.css']
})
export class TopnavComponent implements OnInit {
hideCourse=false;
constructor(private router: Router,private auth:AuthService) { }
changeState(){
this.hideCourse=!this.hideCourse;
}
}
TopnavComponent.html
<button (click)="callMethod()">Submit</button>
<ul class="navbar-nav mr-auto" *ngIf="hideCourse">
<li class="nav-item dropdown">
<a class="nav-link" routerLink="/auth">Sign In</a>
</li>
</ul>
我在我的 Angular 2 网站中有上述组件,我在其中调用 callMethod 来做某些事情,然后调用 topnavComponent 的 changeState 来隐藏 Sign导航栏中的 In 按钮。我得到了分配给 *ngIf 的 hideCourse 属性的正确值,但它没有按需要更新视图。
谁能告诉我我的方案的解决方案。
谢谢。
【问题讨论】:
标签: angular