在我的app.component.html中:
<html>
<head></head>
<body>
<header *ngIf="loggedIn && showHeader"></header>
<router-outlet></router-outlet>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在我的app.component.ts中:
export class AppComponent {
constructor(private HeaderService: HeaderService, private AuthService: AuthService) { }
get loggedIn(): boolean { return this.AuthService.getUserState(); }
get showHeader(): boolean { return this.HeaderService.getToggleState(); }
}
Run Code Online (Sandbox Code Playgroud)
在我的header.service.ts中:
我们创建此服务是因为登录后还有其他特定组件,其中标头也需要隐藏。
@Injectable({
providedIn: 'root'
})
export class HeaderService {
showHeader = true;
constructor() { }
setToggleState(state: boolean) {
this.showHeader = state;
}
getToggleState() { return this.showHeader; }
}
Run Code Online (Sandbox Code Playgroud)
现在,在 my 中login.component,标题应该是不可见的。
现在发生的事情是,有一个短暂的闪烁时刻(似乎是在您登录然后注销并返回登录页面时发生),标题在隐藏之前是可见的(是的,也会抛出 …
我有这个:
$('.task').on('click', function()
{
task_id = $(this).data('id');
console.log('Task id: ' + task_id);
});
Run Code Online (Sandbox Code Playgroud)
但是当通过ajax重新加载内容时,这不起作用。task_id即使在 ajax 重新加载后单击不同元素后,值也保持不变。显然我必须将它绑定到body一个。
这就是我现在代码中的情况(它按预期工作):
$(document).ajaxComplete(function(event, xhr, settings) {
$('.task').on('click', function()
{
task_id = $(this).data('id');
console.log('Task id: ' + task_id);
});
});
Run Code Online (Sandbox Code Playgroud)
但有人告诉我这会重复/加倍触发.on('click')事件?这是真的?我怎么知道什么时候绑定到直接选择器或绑定到document.body?哪个会更有效率?绑定到body事件委托或将事件委托放入其中ajaxComplete()?
我也有点困惑,因为我在同一个.js文件中有其他事件处理程序,但ajaxComplete()即使在 ajax 重新加载后,这些事件处理程序似乎也能正常工作?