Search code examples
angularjsng-class

How to access "lang" attribute to create ng-class expression?


I want to apply a css class only if the lang attribute resolves to de in angularjs. My div looks like:

<div lang="de" ng-class="{ 'myDEclass' : lang === 'de' }">

How can I get the value of the lang attribute to get a valid expression the angular way?


Solution

  • You can add 'global' variables to your $rootScope object to make it available everywhere:

    angular.module("myModule")
        .config(function($rootScope){
            $rootScope.lang = "de";
        });
    

    then in your view (note the $root):

    <div lang="de" ng-class="{ 'myDEclass' : $root.lang === 'de' }">