1

I want to set the direction of the body-element depending on some logic inside the controller.

So if a language file has a certain value, i want to change from "ltr" to "rtl".

I know there is a way of setting HTML attributes via ng-attr-, but it's not working for dir.

I made a JSFiddle to show my problem. The question is:

How can I set the dir-attribute via the controller?

<div ng-controller="MyCtrl">
    <div ng-attr-dir="{{direct}}">   
        <p>Test</p>
    </div>
</div>

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.direct = "rtl";
}
Community
  • 1
  • 1
ohboy21
  • 4,259
  • 8
  • 38
  • 66

4 Answers4

4

Just use dir, instead of ng-attr-dir.

<div dir="{{direct}}"> 

Fiddle

msapkal
  • 8,268
  • 2
  • 31
  • 48
0

It can be accomplished with a trivial directive:

myApp.directive("myDir", function() {
    return {
        link: function(scope, elem, attrs) {
            attrs.$observe("myDir", function(newval) {
                if( newval ) {
                    elem.attr("dir", newval);
                }
                else {
                    elem.removeAttr("dir");
                }
            });
        }
    };
});

Use it as:

<div my-dir="{{direct}}">

A forked fiddle: http://jsfiddle.net/34ch4qef/1/

Nikos Paraskevopoulos
  • 39,514
  • 12
  • 85
  • 90
0

Use inline style="direction:{{direct}}" instead .

http://jsfiddle.net/HB7LU/7373/

Giannis Grivas
  • 3,374
  • 1
  • 18
  • 38
0

You have to use an Angular version the actually supports ngAttr ;) 1.0 doesn't.

a better oliver
  • 26,330
  • 2
  • 58
  • 66