简体   繁体   English

为什么我通过 javascript 的 css 样式不适用于桌面屏幕大小?

[英]Why my css style via javascript are not applying on desktop screen size?

I have an angular7 app in which i am using ngx-dialogs .我有一个angular7应用程序,我在其中使用ngx-dialogs I am using this modal for confirmation modal for delete purpose.我正在使用此模态进行确认模态以进行删除。 I open modal to prompt user that "you want to sure to delete", if user click on "Yes" so item is deleted and modal should be close.我打开模态提示用户“您要确保删除”,如果用户单击“是”,则项目被删除并且模态应该关闭。 I have this implementation in my component.ts我的component.ts有这个实现

import { Ngxalert } from 'ngx-dialogs';

// after class intialization
confirmAlert: any = new Ngxalert;

        delete = (rowData: Users) => {
    if (rowData) {
        this.confirmAlert.create({
            title: 'Delete Warning',
            message: 'Are you sure, you want to delete item?',
            confirm: () => {
                this._dataService.delete(this._constant.user + '/' + rowData._id)
                    .subscribe(res => {
                        console.log('delete response : ',res);
                        console.log('html element',(<HTMLElement>document.querySelector('.ngx-dialog')));
                        (<HTMLElement>document.querySelector('.ngx-dialog')).style.display = "none";
                        this._utilityService.hideSpinner();
                        if (res) {
                            res.success ? this._utilityService.showToster(res.message, 'Notification', 'success') : this._utilityService.showToster(res.message, 'Notification', 'danger');
                            // this.getUsers();
                        }else{
                            (<HTMLElement>document.querySelector('.ngx-dialog')).style.display = "none";
                        }
                        this.getUsers();
                        this._utilityService.hideSpinner();
                    }, error => {
                        this._utilityService.hideSpinner();
                        (<HTMLElement>document.querySelector('.ngx-dialog')).style.display = "none";
                        console.log('User Delete Error : ', error);
                        // this._popupService.OpenError('Having some issue..!');
                        this._utilityService.showToster('Having Some Issue..!', 'Warning', 'warning');
                        this.getUsers();
                    })
            },
        })
    }
}

In this delete function when i received response from sever so i close that modal using this在此删除功能中,当我收到来自服务器的响应时,我使用此功能关闭了该模式

(<HTMLElement>document.querySelector('#ngxdialog-1')).style.display = "none";

And it modal is closing only if i open inspect element or if i resize my chrome to smaller screen.并且仅当我打开检查元素或将 chrome 调整为较小的屏幕时,它才会关闭。 But it's not closing modal on desktop screen.但它并没有在桌面屏幕上关闭模态。 I don't know why is it happening.我不知道为什么会这样。 If it is closing modal on smaller screen so it should also close modal on desktop screen.如果它在较小的屏幕上关闭模态,那么它也应该在桌面屏幕上关闭模态。 It closes the modal if i delete item when inspect element.如果我在检查元素时删除项目,它会关闭模式。 Please refer this video here在此处参考此视频

The issue is the code inside .subscribe() does not trigger an update in the html.问题是.subscribe()中的代码不会触发 html 中的更新。 A fix is using the ngZone provider.解决方法是使用 ngZone 提供程序。

You can try running your code inside Angular NgZone :您可以尝试在Angular NgZone 中运行您的代码:

import { Ngxalert } from 'ngx-dialogs';
import { NgZone } from '@angular/core';

constructor(private ngZone: NgZone) {  }

// after class intialization
confirmAlert: any = new Ngxalert;

    delete = (rowData: Users) => {
    if (rowData) {
        this.confirmAlert.create({
            title: 'Delete Warning',
            message: 'Are you sure, you want to delete item?',
            confirm: () => {
                this._dataService.delete(this._constant.user + '/' + rowData._id)
                    .subscribe(res => {
                    this.ngZone.run(() => {
                        console.log('delete response : ',res);
                        console.log('html element',(<HTMLElement>document.querySelector('.ngx-dialog')));
                        (<HTMLElement>document.querySelector('.ngx-dialog')).style.display = "none";
                        this._utilityService.hideSpinner();
                        if (res) {
                            res.success ? this._utilityService.showToster(res.message, 'Notification', 'success') : this._utilityService.showToster(res.message, 'Notification', 'danger');
                            // this.getUsers();
                        }else{
                            (<HTMLElement>document.querySelector('.ngx-dialog')).style.display = "none";
                        }
                        this.getUsers();
                        this._utilityService.hideSpinner();
                    });
                    }, error => {
                        this._utilityService.hideSpinner();
                        (<HTMLElement>document.querySelector('.ngx-dialog')).style.display = "none";
                        console.log('User Delete Error : ', error);
                        // this._popupService.OpenError('Having some issue..!');
                        this._utilityService.showToster('Having Some Issue..!', 'Warning', 'warning');
                        this.getUsers();
                    })
            },
        })
    }
}

You can find the issue here: https://github.com/angular/angular/issues/31749你可以在这里找到问题: https : //github.com/angular/angular/issues/31749

It sounds like the modal is only closing on resize because resize is when the browser has to re-render, which applies the styles such as display:none.听起来模态仅在调整大小时关闭,因为调整大小是浏览器必须重新渲染的时候,它应用了诸如 display:none 之类的样式。 I'm not super familiar with Angular 7 but I think the answer is to trigger a render.我对 Angular 7 不是很熟悉,但我认为答案是触发渲染。 Can you get rid of the tag to manipulate the DOM directly?可以去掉标签直接操作DOM吗? That tag may refer to a virtual DOM.该标签可能指的是虚拟 DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM