2

Hi I am developing web application in angularjs SPA application which has English and Arabic languages. For RTL and LTR properties I am trying to add class to body html element in app.js as below.

 $scope.changeLanguage = function (lang) {
        $scope.lang = lang == 'de_AR' ? 'de_AR' : 'de_EN';
        var myEl = angular.element(document.querySelector('#body'));
        myEl.addClass('rtl');
}

Using above code I am not able to add class. I can see below code in html(browser).

<body ng-controller="RoslpAppController" class="ng-scope">

May I know what is the correct way to add css classes using Angularjs?

2

4 Answers 4

3

Below is for ternary check in ng class

<div ng-class="(lang === 'de_AR') ? 'rtl' : 'ltr'"></div>

This is for multiple cases in ng class for one condition

<div ng-class="{'de_AR':'rtl', 'de_EN':'ltr'}[lang]"></div>

Code inside controller

$scope.changeLanguage = function (lang) {
        $scope.lang = lang == 'de_AR' ? 'de_AR' : 'de_EN';
        var myEl = angular.element(document.querySelector('#body'));
        myEl.addClass('rtl');
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks i have added this ng-class="(lang === 'de_AR') ? 'rtl' : 'ltr'" and html i can see <body ng-controller="RoslpAppController" ng-class="(lang === 'de_AR') ? 'rtl' : 'ltr'" class="ng-scope rtl">. Is this is correct?
Yes correct. ng-scope is default class added by angular
3

You are looking for ngClass directive . if you have a scope variable $scope.rtl that drives what part of your content is displayed, you can use ng-class to add dynamic classes:

 <body ng-controller="MainCtrl" ng-class="{'rtl'}">

DEMO

3 Comments

Thanks Sajeetharan. How can i switch the classes? If the language is arabic then rtl and if the language is english the ltr. How can i do this?
add a ternary operator to check stackoverflow.com/questions/15397252/…
I have added ng-class="{{dir}}" and from controller $scope.dir=ltr i have done. Is this is the correct way i am following?
1

you can use ng-class

<body ng-controller="RoslpAppController" ng-class="{'class1':lang == 'de_AR', 'class2': lang == 'de_EN'} ng-scope">

1 Comment

Thank you. I have added ng-class="{lang == 'de_AR':rtl,lang == 'de_EN': ltr}" and i got Syntax Error: Token '==' is unexpected, expecting [:] at column 7 of the expression [{lang == 'de_AR':rtl,lang == 'de_EN': ltr}] starting at [== 'de_AR':rtl,lang == 'de_EN': ltr}]. May i know what will be the correct syntex?
0

Your code does not work because

angular.element(document.querySelector('#body'));

looks for an element with the id 'body' if you want to acces the body, use:

angular.element(document.querySelector('body'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.