0

I was thinking how to change class and add HTML if user pick language (see code below)

<li>
 <a ng-click="changeLanguage('en')">English</a>
</li>

to something like this:

<li>
 <a ng-click="changeLanguage('en')" class="active">English
  <span class="icon icon-tick"></span>
 </a>
</li>

My first idea was something like to add something like $scope.active and then use ng-class

ng-class="{'active': active, '': !active}"

But this not work with that HTML below and I'm also not sure if it work correctly with my class.

Can someone advise me how to do things like this in Angular? Thanks

EDIT: I wasn't specific enaugh, i have more languages in option so HTML looks like:

              <ul class="reset">
                    <li>
                        <a ng-click="changeLanguage('en')">English</a>
                    </li>
                    <li>
                        <a ng-click="changeLanguage('cs')" class="active">
                            Česky
                            <span class="icon icon-tick"></span>
                        </a>
                    </li>
                </ul>

Where "CS" is how it should always look like when some language is active and EN is how it should looks like when that language is not active

5 Answers 5

1

What you need to do is store the currently active selection then change your html angular code to check against that like so:

var app = angular.module("myApp", []);
app.controller("test", function($scope) {
  $scope.currentActive = "en";
  $scope.active = function(lang) {
    return lang == $scope.currentActive;
  }
  $scope.changeLanguage = function(lang) {
    $scope.currentActive = lang;
  }
});
.active {
  color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="test">
  <li>
    <a ng-click="changeLanguage('en')" ng-class="{ 'active': active('en') }">English
    <span ng-show="active('en')" class="icon icon-tick">Tick</span>
   </a>
  </li>
  <li>
    <a ng-click="changeLanguage('cs')" ng-class="{ 'active': active('cs') }">
      Česky
      <span ng-show="active('cs')" class="icon icon-tick">Tick</span>
    </a>
  </li>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Just awesome code, thumb up and big thanks to you @Richard Dalton
0

You can use jQuery to do that.

$("a").click(function(){
    $(this).attr("class","active");
});

1 Comment

Hey @Swapnil Shama thank you for your answer. UnfortunatelyI need to do this in angular :)
0

Try this:

<li>
<a ng-click="changeLanguage('en')" ng-class="isActive">English
<span class="icon icon-tick" ></span>
</a>
</li>

And in your function $scope.changeLanguage, add this:

$scope.isActive = 'active';

Edits: Use:

<li>
 <a ng-click="changeLanguage('en')" ng-class="{'active': currentLang =='en'}">English
  <ng-show="currentLang == 'en'" span class="icon icon-tick" ></span>
 </a>
</li><li>
 <a ng-click="changeLanguage('cs')" ng-class="{'active': currentLang =='cs'}">English
  <ng-show="currentLang == 'cs'" span class="icon icon-tick" ></span>
 </a>
</li>

And in your function $scope.changeLanguage, add this:

$scope.currentLang = your_function_param;

1 Comment

Hi @Anass, i wasnt specific enaugh so I am really sorry for that. Please check my EDIT
0

You can also use currentActive === 'cs' directly

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="test">
  <li>
    <a ng-click="changeLanguage('en')" ng-class="{ 'active': currentActive === 'en' }">English
    <span ng-show="active('en')" class="icon icon-tick">Tick</span>
   </a>
  </li>
  <li>
    <a ng-click="changeLanguage('cs')" ng-class="{ 'active': currentActive === 'cs' }">
      Česky
      <span ng-show="currentActive === 'cs'" class="icon icon-tick">Tick</span>
    </a>
  </li>
</div>

Comments

0

Manipulating dom in angular controllers is not recommended. May be u can already have the span with ng-if on it

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.