最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How do I bind this scope variable to a ng-class attribute? - Stack Overflow

matteradmin5PV0评论

This seems like it should be fairly straightforward, however I'm absolutely out of options

In my view I have:

<div>{{selectedTheme.theme}}</div>
<ion-nav-view ng-class="selectedTheme.theme" name="menuContent"></ion-nav-view>

where the first div outputs the class as expected. However, I cannot get the class to get inserted into the ion-nav-view element!

The resulting mark-up is as follows:

<div class="ng-binding">Dark</div>
<ion-nav-view ng-class="selectedTheme.theme" name="menuContent" class="view-container" nav-view-transition="ios" nav-view-direction="none" nav-swipe="">></ion-nav-view>

Any input at all would be immense!

PS: In addition, setting it simply with class works fine.

This seems like it should be fairly straightforward, however I'm absolutely out of options

In my view I have:

<div>{{selectedTheme.theme}}</div>
<ion-nav-view ng-class="selectedTheme.theme" name="menuContent"></ion-nav-view>

where the first div outputs the class as expected. However, I cannot get the class to get inserted into the ion-nav-view element!

The resulting mark-up is as follows:

<div class="ng-binding">Dark</div>
<ion-nav-view ng-class="selectedTheme.theme" name="menuContent" class="view-container" nav-view-transition="ios" nav-view-direction="none" nav-swipe="">></ion-nav-view>

Any input at all would be immense!

PS: In addition, setting it simply with class works fine.

Share Improve this question asked Mar 14, 2015 at 23:45 Louis93Louis93 3,9219 gold badges53 silver badges94 bronze badges 3
  • I say you should go for a directive here and use element.addClass(). You should take a look at this question too – Dan Mindru Commented Mar 14, 2015 at 23:55
  • @JamesP, that is just not correct. ng-class accepts an expression that if evaluated to string of space-delimited class names or an array, uses these values as is. Only, when the expression is an object, the property is used as a class if it has a truthy value – New Dev Commented Mar 15, 2015 at 0:31
  • 1 if you just want the name of the class add {{}} and your good, Like : ng-class="{{selectedTheme.theme}}" – Neta Meta Commented Mar 15, 2015 at 0:46
Add a ment  | 

1 Answer 1

Reset to default 3

In Angular ng-classuses an expression or just a plain old string. You can do things like:

  1. Add/Remove classes based on Angular variables
  2. Add/Remove classes based on evaluated expressions
  3. Bind single or multiple classes based on dynamic data

You can just add an Angular variable to ng-class and that is the class that will be used for that element. So in your case, if selectedTheme.theme yields the exact class name you wish to use, then it should work fine.

<ion-nav-view ng-class="varHoldingClassName" name="menuContent"></ion-nav-view>

So here's a full example in action:

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

function MyCtrl($scope) {
    $scope.name = 'Angular!';
    $scope.selectedTheme = {
         'coalTheme': 'textColor'
    };
}
.textColor{
    color: red;  
}
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp">
<div ng-controller="MyCtrl">
    <div ng-class="selectedTheme.coalTheme">Hello, {{name}}!</div>
</div>
</div>

Post a comment

comment list (0)

  1. No comments so far