【发布时间】:2018-01-22 10:43:37
【问题描述】:
我正在为这个问题而苦苦挣扎,无法弄清楚。 我只需要从我的 navbar.component 中的菜单条目单击显示位于页面中的弹出 div。
我在弹出窗口中添加了一个属性“show”,它使用 ngClass (with if) 指令在我的 div 上打印“show”类。如果操作按钮在我的弹出组件inside 中,我可以让它工作,但我无法打印单击另一个组件的显示类。对象中的属性得到更新,但未打印类。我正在使用 Angular 4 和 ng-bootstrap。我尝试了服务和父/子发出事件。
这是我的情况:
app.component.html
<app-nav-bar></app-nav-bar>
<app-login></app-login>
<router-outlet></router-outlet>
<app-footer></app-footer>
navbar.component.html
...
<button class="dropdown-item" (click)="showPopup()">LOGIN</button>
...
navbar.component.ts
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
@Component({
moduleId: module.id,
selector: 'app-nav-bar',
templateUrl: 'navbar.component.html',
styleUrls: ['./navbar.component.css'],
})
export class NavbarComponent implements OnInit {
@Output() show = new EventEmitter<boolean>();
ngOnInit() {
}
showPopup() {
this.show.emit(true);
}
}
login.component.html
<div id="wrapper-login-popup" class="fade-from-top" [(class.show)]="show">
<div id="container-login-popup">
<div class="row">
<div class="col-sm-12 text-center">
<img id="popup-bomb" src="assets/images/bomb.png" alt="bomb"/>
<img id="popup-close" class="close-icon" src="assets/images/close.png" alt="close"
(click)="closePopup()"/>
</div>
</div>
</div>
</div>
login.component.ts
import {Component, Input, OnInit} from '@angular/core';
import {AuthService} from '../services/auth.service';
import {IUser} from './user';
@Component({
selector: 'app-login',
templateUrl: 'login.component.html',
styleUrls: ['login.css']
})
export class LoginComponent implements OnInit {
private username: string;
private password: string;
@Input() show: boolean = false;
constructor(private AuthService: AuthService) {
}
ngOnInit() {
}
login() {
...
}
showPopup() {
console.log(this); //Show is false
this.show = true;
console.log(this); //Show is true but does not trigger the show class
}
closePopup() {
this.show = false;
}
}
【问题讨论】:
-
你能为同一个@SBO创建plunker吗
-
这听起来可能很傻,但你可以尝试使用像
[ngClass]= " {'show' : show} "这样的ngClass -
您是在开发模式还是生产模式下运行代码?
-
开发。我会理解如何在 Angular 4 中操作非嵌套组件的属性(不使用子/父)
-
如何通知登录组件在导航栏中的显示事件?您已将“显示”列为登录的输入和导航栏的输出,但它们没有连接到我能看到的任何地方。您需要让您的应用组件传递消息或为其提供服务。
标签: javascript angular typescript