javascript - Angularjs add class to body in html - Stack Overflow

Hi I am developing web application in angularjs SPA application which has English and Arabic languages.

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?

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?

Share Improve this question edited Sep 21, 2017 at 4:46 Vivz 6,6302 gold badges18 silver badges34 bronze badges asked Sep 21, 2017 at 4:13 Niranjan GodboleNiranjan Godbole 2,1758 gold badges50 silver badges98 bronze badges 2
  • you can use ng-class for this – Paulo Galdo Sandoval Commented Sep 21, 2017 at 4:16
  • You can refer weblog.west-wind./posts/2015/may/23/… – Vivz Commented Sep 21, 2017 at 5:18
Add a ment  | 

4 Answers 4

Reset to default 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');
}

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

you can use ng-class

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

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'));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744235206a4564440.html

相关推荐

  • javascript - Angularjs add class to body in html - Stack Overflow

    Hi I am developing web application in angularjs SPA application which has English and Arabic languages.

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信