我想在用户离开我的angular 2应用程序的特定页面之前警告用户未保存的更改.通常我会使用window.onbeforeunload,但这不适用于单页面应用程序.
我发现在角度1中,你可以挂钩$locationChangeStart事件confirm为用户抛出一个盒子,但我还没有看到任何显示如何使角度2工作,或者如果该事件仍然存在.我也见过为ag1提供功能的插件onbeforeunload,但同样,我还没有看到任何方法将它用于ag2.
我希望其他人找到解决这个问题的方法; 任何一种方法都可以用于我的目的.
也许我是愚蠢的,但我不能为我的生活弄清楚如何获得自定义表单验证,从验证失败的情况被称为停止的onsubmit.在创建新的Control时,我尝试使用HTML语法(通过将自定义验证关键字直接添加到表单组件的htmlTemplate中)以及代码.我还没有看到任何暗示此功能不适用于自定义验证器的内容.
这是我正在使用的代码示例
import { Component, Output, EventEmitter, OnInit } from 'angular2/core';
import { FORM_DIRECTIVES, FormBuilder, Control, ControlGroup} from 'angular2/common';
@Component({
selector: 'formality-form',
templateUrl: 'html/formality.html',
styleUrls: ['styles.css']
})
export class FormalForm implements OnInit {
formGroup: ControlGroup;
// Here I register the custom validator to the Control group
constructor(fb: FormBuilder) {
this.formGroup = fb.group({
'date': ['']
} {validator: FormalForm.customVal});
}
// This is my custom validator
public static customVal(control: ControlGroup){
return {isFail:true};
}
// I would like for this to …Run Code Online (Sandbox Code Playgroud)