这是我的问题,我有两个视图(View1和View2)和每个视图的控制器(Ctrl1和Ctrl2).在View1中,我试图在用户意外离开页面之前警告用户而不保存更改.
我正在使用window.onbeforeunload,它工作正常,但我的问题是即使我只在一个控制器上进行onbeforeunload,事件似乎也在第二个控制器中触发!但我在那里甚至没有那件事.当用户离开页面并且有未保存的更改时,他会被onbeforeunload警告,如果用户关闭警报并离开页面,他是否会被带回第二个视图,如果用户现在试图离开这个其他视图,我们也会收到有关未保存更改的警告!当情况不是这样的时候!为什么会这样?
我也使用$ locationChangeStart,它运行得很好,但只有当用户更改路线时,不是当他们刷新浏览器,点击"返回"或尝试关闭它时,这就是为什么我被迫使用onbeforeunload(如果你有更好的方法,请告诉我.任何帮助或更好的方法将不胜感激!
//In this controller I check for window.onbeforeunload
app.controller("Ctrl1", function ($scope, ...)){
window.onbeforeunload = function (event) {
//Check if there was any change, if no changes, then simply let the user leave
if(!$scope.form.$dirty){
return;
}
var message = 'If you leave this page you are going to lose all unsaved changes, are you sure you want to leave?';
if (typeof event == 'undefined') {
event = window.event;
}
if (event) {
event.returnValue = message;
}
return message; …Run Code Online (Sandbox Code Playgroud) 我对 Angular 2 还是很陌生,希望你们能帮助我。
我有一个相当简单的应用程序,有一个登录页面,成功登录后,用户被定向到一个带有侧菜单的页面。登录屏幕没有此侧边菜单。当用户注销时,他会再次被定向到登录页面。
问题是登录后侧菜单变得可见,但其他内容只有在刷新后才可见。注销也是一样,注销后页面是空白的,只有在刷新内容(登录页面)后才会显示。我可能做错了什么,但即使在查看其他问题后我也无法弄清楚。
这是我的路由:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ProfileComponent } from './profile-component/profile-component.component'
import { PageNotFoundComponent } from './page-not-found/page-not-found.component';
import { LoginComponent } from './login/login.component';
import { LoggedInGuard } from './logged-in/logged-in.guard';
const appRoutes: Routes = [
{path: 'profile', component: ProfileComponent, canActivate: [LoggedInGuard]},
{path: 'login', component: LoginComponent},
{path: '', component: LoginComponent},
{path: '**', component: PageNotFoundComponent},
];
@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
],
exports: [
RouterModule
]
})
export class …Run Code Online (Sandbox Code Playgroud)