如何在包服务中使用document.dir

问题描述 投票:0回答:1

我需要设置rtl和ltr的语言和方向。因此在服务中注入了DOCUMENT,在核心模块中提供了服务。

最后我得到了Bild程序包,并在项目中导入了CoreModule

CoreModule:

this._translateService.addLangs(supportLang);

this._translateService.setDefaultLang(defaultLang);
this._translateService.use(defaultLang).subscribe(res => {

            const dir = defaultLang === 'ar' ? 'rtl' : 'ltr';
            this._uiService.setDirection(dir);
            this._translateService.isCompleted();

});

UiService

export class UiService {

        @NarikInject(DOCUMENT)
        document: Document;

        @NarikInject(Location)
        _location: Location;

        setDirection(dir: string) {
            this.document.dir = dir;
        }
    }

@ Narik在github中注入

core.js:6014错误TypeError:无法将属性'dir'设置为null

angular dom document ng-packagr
1个回答
0
投票

在您尝试分配其dir属性时,文档为null。尝试在ngOnInit内执行此操作。

您还需要检查是否设置了与角度管理相同的服务实例的目录

© www.soinside.com 2019 - 2024. All rights reserved.