【发布时间】:2017-11-18 18:22:10
【问题描述】:
我有一个有用户登录和注销的 Angular 应用程序。在用户登录之前,我将欢迎页面显示为主页。我只想在欢迎页面上启用背景图像。用户登录后,背景图像必须消失。当用户注销时,他将被重定向到欢迎页面,该页面必须再次显示背景图像。
我尝试通过将选择器重命名为“body”来在 app.component.ts 中使用 @HostBinding。
app.component.ts
import {Component, HostBinding, Input} from '@angular/core';
import {InputMask} from "primeng/primeng";
@Component({
selector: 'body',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
path = '../assets/img/AvayaHome.jpg';
title = 'app';
toggleClass = true;
@HostBinding('class.bodyClass') isWelcomePage = this.toggleClass;
}
这是我的 CSS。
app.component.css
.bodyClass {
background-image: url("../assets/img/AvayaHome.jpg");
}
这是我的 index.html
<!doctype html>
<html lang="en">
<head>
<title> Something </title>
</head>
<body class="bodyClass">
<app-welcome-page></app-welcome-page>
</body>
</html>
我通过将 toggleClass 指定为 true 来启用 bodyClass 的 css 样式。用户登录后,我将更改子组件中的 toggleClass(位于 app.component.ts 中)的值。
这是我的 login.component.ts
onLogin() {
console.log('onLogin() invoked:', this._email, ':' , this.password);
if (this._email == null || this.password == null) {
this.errorMessage = 'All fields are required';
return;
}
this.errorMessage = null;
this.loginservice.authenticate(this._email, this.password);
this.appComponent.toggleClass = true;
this.router.navigate(['/dashboard']);
}
toggleClass 的值在用户登录到 FALSE 时发生变化。但我仍然看到背景图像。不知道我做错了什么。任何帮助将不胜感激。
【问题讨论】:
标签: javascript html css angular