Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use <code>ng-style</code> to dynamically change a CSS class property using AngularJS.</p> <p>See the code below or this plunk (<a href="http://plnkr.co/edit/n4NlIfgfXIiNHWu5oTzS?p=preview" rel="nofollow noreferrer">http://plnkr.co/edit/n4NlIfgfXIiNHWu5oTzS?p=preview</a>)</p> <p>I've created an array of colours to be used by the <code>ng-repeat</code> and change the <code>background-color</code> of each item dynamically.</p> <p>Note that although all the items have a class called <code>original</code> with red background, that value is updated (override) with a new colour from the array.</p> <p>So now you're probably thinking: "<em>Cool! If I can change the class color property dynamically I should be able to do the same with any other property, like the <code>content</code> one right ?!?</em>"</p> <p>The answer is "<strong><em>Yes &amp; No</em></strong>".</p> <p>It seems pseudo selectors like <code>:after</code> and <code>:before</code> are treated differently.</p> <blockquote> <p>"Although they are rendered by browsers through CSS as if they were like other real DOM elements, pseudo-elements themselves are not part of the DOM, and thus you can't select and manipulate them directly with AngularJS, jQuery (or any JavaScript APIs for that matter"</p> </blockquote> <p>You can find a full explanation on this post: (<a href="https://stackoverflow.com/a/5041526/1310945">https://stackoverflow.com/a/5041526/1310945</a>)</p> <p>Have said that, I believe you can probably manage to work around this, using this solution (<a href="https://stackoverflow.com/a/16507264/1310945">https://stackoverflow.com/a/16507264/1310945</a>).</p> <p>I haven't tried yet - but I might do another time just for fun.</p> <p>On a side note, maybe there's a better solution ( and more AngularJs style approach) for what you're trying to do using <code>ng-class</code>.</p> <p>Anyway hope this at least send you on the right direction. :)</p> <pre><code>&lt;!doctype html&gt; &lt;html lang="en" ng-app="myApp"&gt; &lt;head&gt; &lt;meta charset="UTF-8"&gt; &lt;title&gt;Document&lt;/title&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"&gt;&lt;/script&gt; &lt;link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootswatch/3.0.0/united/bootstrap.min.css"&gt; &lt;style&gt; ul { list-style-type: none; color: #fff; } li { padding: 10px; text-align: center; } .original { background-color: red; } &lt;/style&gt; &lt;script&gt; var myApp = angular.module('myApp', []); myApp.controller("myAppCtrl", ["$scope", function($scope) { $scope.colors = ['#C1D786', '#BF3978', '#15A0C6', '#9A2BC3']; $scope.style = function(value) { return { "background-color": value }; } }]); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div ng-controller="myAppCtrl"&gt; &lt;div class="container"&gt; &lt;div class="row"&gt; &lt;div class="span12"&gt; &lt;ul&gt; &lt;li ng-repeat="color in colors"&gt; &lt;h4 class="original" ng-style="style(color)"&gt; {{ color }}&lt;/h5&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload