Angular2 material sidenav - changing rtl/ltr direction dynamically

Deadly 提交于 2019-12-06 09:27:09

Take a look at source code Dir directive

@Input('dir') _dir: LayoutDirection = 'ltr';

As you can see you're changing _dir property instead of dir setter:

set dir(v: LayoutDirection) {
  let old = this._dir;
  this._dir = v;
  if (old != this._dir) {
    this.dirChange.emit(null);
  }
}

So i think your solution should be like:

view

<md-sidenav-layout fullscreen dir="ltr" #root="$implicit">

<select #langSelect (change)="changeLang(langSelect.value, root)">

component

changeLang(lang, root: Dir) {
  this.translate.use(lang);
  root.dir = lang == "fr" ? "rtl" : "ltr";
}

This way you can omit direction: string property on your component.

And one note:

translate: TranslateService; //it's redundant `private translate: TranslateService` does it
direction: string; // omit it

constructor(private translate: TranslateService) {
  this.direction = "ltr"; // omit it
  translate.addLangs(["en", "fr"]);
  translate.setDefaultLang('en');

  let browserLang = translate.getBrowserLang();
  translate.use(browserLang.match(/en|fr/) ? browserLang : 'en');
  this.translate = translate; // it's redundant
}

Here's your Plunker Example

If you think that this is the wrong decision then check this

Hope this will help you!

I've been trying to use the "end" and "start" align attribute to solve the problem, but I don't know why it's not possible. I think a possible solution is that you "play" a bit with the NgStyle and the margin-right as I show in the following Plnkr

[ngStyle]="{'margin-right': move}"

example

Hope this could be useful.

I am not sure if what I did with angular1 also works with angular 2. That is just adding a CSS:

md-sidenav {
    left: initial;
    right: 0;
}
md-sidenav.md-closed {
    transform: translate3d(100%, 0, 0);
}

To now have it only for those with rtl-direction, adding a class/attribute to the body and then only selecting that, would be sufficient.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!