GVKun编程网logo

如何使用angular2材质对话框阻止canDeactivate(angular提示框)

11

本文将带您了解关于如何使用angular2材质对话框阻止canDeactivate的新内容,同时我们还将为您解释angular提示框的相关知识,另外,我们还将为您提供关于android–材质对话框库–

本文将带您了解关于如何使用angular2材质对话框阻止canDeactivate的新内容,同时我们还将为您解释angular提示框的相关知识,另外,我们还将为您提供关于android – 材质对话框库 – 防止在onPositive函数调用上关闭/关闭对话框、Angular - 如何避免在 canActivate 中进行多次 http 调用、Angular 12 中的材质对话框显示没有样式、Angular CanDeactivate Guard 无法与 MatDialog 正常工作的实用信息。

本文目录一览:

如何使用angular2材质对话框阻止canDeactivate(angular提示框)

如何使用angular2材质对话框阻止canDeactivate(angular提示框)

如果用户从具有脏表单的页面导航,我成功使用canDeactivate提供警告消息:

我正在使用的代码在这里:

is_submit = false;
   canDeactivate() {
       //https://scotch.io/courses/routing-angular-2-applications/candeactivate
        if (this.myForm.dirty == true && this.is_submit==false){
            return window.confirm('discard changes?');
        } //end if
        return true
    } // end  canDeactivate

这是我得到代码的地方:

https://scotch.io/courses/routing-angular-2-applications/candeactivate

但是我想使用angular2对话框.这是我的代码:

//ts for the main component

    is_submit = false;
   canDeactivate() {
        if (this.myForm.dirty == true && this.is_submit==false){
            const config = new mddialogConfig();
            config.disableClose=true;
            let dialogRef = this.dialog.open(DialogCanDeactive,config);
            dialogRef.afterClosed().subscribe(result => {
                if (result=='cancel'){
                    return false;
                } 
                if (result=='save'){
                    return true;
                } 
                if (result=='discard'){
                    return true;
                } 
            }); //end dialogRef
        } //end if
        return true
    }

 ///Code for the dialog

@Component({
  selector: 'can_deactive_dialog',template: `
<div>
    <button md-raised-button (click)="dialogRef.close('cancel')">Cancel</button>
    <button md-raised-button (click)="dialogRef.close('save')">Save Changes</button>
    <button md-raised-button (click)="dialogRef.close('discard')">discard Changes</button>
</div>
`,})
export class DialogCanDeactive {


  constructor(public dialogRef: mddialogRef<DialogCanDeactive>) {} //end constructor

}

当我离开时会发生什么:

1)我转到导航页面

2)然后对话显示..

如何使用以下代码的Dialog块?

window.confirm('discard changes?')
canDeactivate方法也可以返回Promise或Observable.您应该返回该值并解析promise或使用您想要的结果在observable上发出值.

在您的具体示例中,您可以从afterClosed方法返回observable而不是订阅它,并将其映射到布尔值:

return dialogRef.afterClosed().map(result => {
                if (result=='cancel'){
                    return false;
                } 
                if (result=='save'){
                    return true;
                } 
                if (result=='discard'){
                    return true;
                } 
            }).first();

另外,我会从保护中移出这个逻辑,例如在组件中,然后从那里调用一个方法.

android – 材质对话框库 – 防止在onPositive函数调用上关闭/关闭对话框

android – 材质对话框库 – 防止在onPositive函数调用上关闭/关闭对话框

我正在使用 this Material Dialog库,当我单击positive按钮时,将调用onPositive函数并关闭对话框.如何阻止对话框关闭/解除?

谢谢你的回答.

解决方法

add:autodismiss(false)
new MaterialDialog.Builder(mainActivity)
            .title(R.string.title)
            .autodismiss(false)
            .content(R.string.content)
            .positiveText(R.string.positive)
            .negativeText(R.string.negative)
            .positiveColor(setColor())
            .callback(new MaterialDialog.ButtonCallback() {
                @Override
                public void onPositive(MaterialDialog dialog) {

                }
            })
            .negativeColor(setColor())
            .typeface(titleAndActions,contentAndListItems)
            .build()
            .show();

Angular - 如何避免在 canActivate 中进行多次 http 调用

Angular - 如何避免在 canActivate 中进行多次 http 调用

export class AuthGuardService implements CanActivate {
  constructor(
    public auth: AccountService
    public router: Router
  ) {}

  canActivate(): Observable<boolean> {
    return this.auth.identity().pipe(
      map(account => {
        if (account) {
          return true;
        }
        this.router.navigate(['welcome']);
        return false;
      })
    );
  }
}

@Injectable()
export class StartupService implements CanActivate {
  constructor(
    private auth: AccountService,private router: Router,private sidebarService: SidebarService
  ) {}

  canActivate(): Observable<boolean> {
    return this.auth.identity().pipe(
      switchMap(account => this.sidebarService.getCompanies(account.id.toString())
        .pipe(
          map(firms => {
            if(account.isAdmin && Object.keys(firms).length === 0) {
              this.router.navigate(['startup']);
              return false;
            }
            return true;
          }),catchError(() => of(false)) // you probably also want some redirection on error
        )
      ),);
  }
}

在您的设置中更重要的是您如何实施AccountService.identity 如果它返回一些具有当前用户身份状态的 observable,则如下所示:

class AccountService {
  identity$ = new ReplaySubject(1);

  identity() {
    return this.identity$;
  }

  authenticate() {
    this.http.post('/authentication').subscribe((identity) => {
      this.identity$.next(identity)
    })
  }

  logout() {
    this.identity$.next(null)
  }
}

仅当您调用身份验证时才会调用 HTTP,并且您的用户身份将存储在 identity$

Angular 12 中的材质对话框显示没有样式

Angular 12 中的材质对话框显示没有样式

如何解决Angular 12 中的材质对话框显示没有样式?

我刚刚为对话框创建了组件,例如:https://v6.material.angular.io/components/dialog/examples

但它显示没有样式:

enter image description here

这是我的代码: 模板列表组件.html:

<mat-dialog-content>
    <p>
        {{message}}
    </p>
</mat-dialog-content>
<mat-dialog-actions align="center">
    <button mat-raised-button color="primary" (click)="onConfirmClick()" tabindex="1">{{confirmButtonText}}</button>
    <button mat-raised-button mat-dialog-close tabindex="-1">{{cancelButtonText}}</button>
</mat-dialog-actions>

确认对话框.ts:

import { Component,Inject } from ''@angular/core'';
import { MatDialogRef,MAT_DIALOG_DATA } from ''@angular/material/dialog'';

@Component({
  selector: ''confirmation-dialog'',templateUrl: ''confirmation-dialog.html'',})
export class ConfirmationDialog {
  message: string = "Are you sure?"
  confirmButtonText = "Yes"
  cancelButtonText = "Cancel"
  constructor(
    @Inject(MAT_DIALOG_DATA) private data: any,private dialogRef: MatDialogRef<ConfirmationDialog>) {
      if(data){
    this.message = data.message || this.message;
    if (data.buttonText) {
      this.confirmButtonText = data.buttonText.ok || this.confirmButtonText;
      this.cancelButtonText = data.buttonText.cancel || this.cancelButtonText;
    }
      }
  }

  onConfirmClick(): void {
    this.dialogRef.close(true);
  }

}

打开对话框功能如下所示:

openDialog() {
    const dialogRef = this.dialog.open(ConfirmationDialog,{
      data:{
        message: ''Are you sure want to delete?'',buttonText: {
          ok: ''Save'',cancel: ''No''
        }
      }
    });

我从 app.module.ts

中的材料中导入了所有模块

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Angular CanDeactivate Guard 无法与 MatDialog 正常工作

Angular CanDeactivate Guard 无法与 MatDialog 正常工作

这正是你描述的方式。

您的路由守卫期望立即为真或假。它无法处理可观察对象。

所以我的建议是执行以下操作。

第一

检查一下,但是当组件脏时直接返回 false,以便路由保护防止重新路由。构建一个服务,该服务存储新路由并包含一个名为 reroutingAllowed 的字段。最初设置为 false

canDeactivate(component: DirtyComponent): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return component?.isDirty();
}

confirmRouteChange() {
    // here we already take the new field into account
    if (this.isDirty && !reroutingAllowed) {
        let dialogRef = this.matDialog.open(AlertModalComponent,{
            data: {
                msg: ''Your changes have not been saved. If you leave this page,your changes will be lost.'',title: ''Changes have not been saved'',class: ''save-changes''
            }
        });

        // directly return false
        return false;
    } else {
        return true;
    }
}

第二

在您的服务中并行记住用户想要前往的新路线目标,以便在用户在模态对话框中满足其决定时访问它。

第三

等待用户回答对话框。如果他想离开当前页面,将变量 reroutingAllowed 设置为 true 并触发 Angular 路由器再次调用新的路由目标。

这次守卫会让用户通过,尽管表单仍然很脏。

您只需要考虑如何设置上述服务以及如何获取和记住新的路由目标。

今天关于如何使用angular2材质对话框阻止canDeactivateangular提示框的介绍到此结束,谢谢您的阅读,有关android – 材质对话框库 – 防止在onPositive函数调用上关闭/关闭对话框、Angular - 如何避免在 canActivate 中进行多次 http 调用、Angular 12 中的材质对话框显示没有样式、Angular CanDeactivate Guard 无法与 MatDialog 正常工作等更多相关知识的信息可以在本站进行查询。

本文标签: