EVOLUTION-MANAGER
Edit File: ngClass.html
<a href='https://github.com/angular/angular.js/edit/v1.3.x/src/ng/directive/ngClass.js?message=docs(ngClass)%3A%20describe%20your%20change...#L117' class='improve-docs btn btn-primary'><i class="glyphicon glyphicon-edit"> </i>Improve this Doc</a> <a href='https://github.com/angular/angular.js/tree/v1.3.9/src/ng/directive/ngClass.js#L117' class='view-source pull-right btn btn-primary'> <i class="glyphicon glyphicon-zoom-in"> </i>View Source </a> <header class="api-profile-header"> <h1 class="api-profile-header-heading">ngClass</h1> <ol class="api-profile-header-structure naked-list step-list"> <li> - directive in module <a href="api/ng">ng</a> </li> </ol> </header> <div class="api-profile-description"> <p>The <code>ngClass</code> directive allows you to dynamically set CSS classes on an HTML element by databinding an expression that represents all classes to be added.</p> <p>The directive operates in three different ways, depending on which of three types the expression evaluates to:</p> <ol> <li><p>If the expression evaluates to a string, the string should be one or more space-delimited class names.</p> </li> <li><p>If the expression evaluates to an array, each element of the array should be a string that is one or more space-delimited class names.</p> </li> <li><p>If the expression evaluates to an object, then for each key-value pair of the object with a truthy value the corresponding key is used as a class name.</p> </li> </ol> <p>The directive won't add duplicate classes if a particular class was already set.</p> <p>When the expression changes, the previously added classes are removed and only then the new classes are added.</p> </div> <div> <h2>Directive Info</h2> <ul> <li>This directive executes at priority level 0.</li> </ul> <h2 id="usage">Usage</h2> <div class="usage"> <ul> <li>as attribute: <pre><code><ANY ng-class=""> ... </ANY></code></pre> </li> <li>as CSS class: <pre><code><ANY class="ng-class: ;"> ... </ANY></code></pre> </li> </div> <h2 id="animations">Animations</h2> <p>add - happens just before the class is applied to the element remove - happens just before the class is removed from the element</p> <a href="api/ngAnimate/service/$animate">Click here</a> to learn more about the steps involved in the animation. <section class="api-section"> <h3>Arguments</h3> <table class="variables-matrix input-arguments"> <thead> <tr> <th>Param</th> <th>Type</th> <th>Details</th> </tr> </thead> <tbody> <tr> <td> ngClass </td> <td> <a href="" class="label type-hint type-hint-expression">expression</a> </td> <td> <p><a href="guide/expression">Expression</a> to eval. The result of the evaluation can be a string representing space delimited class names, an array, or a map of class names to boolean values. In the case of a map, the names of the properties whose values are truthy will be added as css classes to the element.</p> </td> </tr> </tbody> </table> </section> <h2 id="example">Example</h2><p>Example that demonstrates basic bindings via ngClass directive. <div> <a ng-click="openPlunkr('examples/example-example62')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-example62"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><p ng-class="{strike: deleted, bold: important, red: error}">Map Syntax Example</p> <input type="checkbox" ng-model="deleted"> deleted (apply "strike" class)<br> <input type="checkbox" ng-model="important"> important (apply "bold" class)<br> <input type="checkbox" ng-model="error"> error (apply "red" class) <hr> <p ng-class="style">Using String Syntax</p> <input type="text" ng-model="style" placeholder="Type: bold strike red"> <hr> <p ng-class="[style1, style2, style3]">Using Array Syntax</p> <input ng-model="style1" placeholder="Type: bold, strike or red"><br> <input ng-model="style2" placeholder="Type: bold, strike or red"><br> <input ng-model="style3" placeholder="Type: bold, strike or red"><br></code></pre> </div> <div class="runnable-example-file" name="style.css" language="css" type="css"> <pre><code>.strike { text-decoration: line-through; } .bold { font-weight: bold; } .red { color: red; }</code></pre> </div> <div class="runnable-example-file" name="protractor.js" type="protractor" language="js"> <pre><code>var ps = element.all(by.css('p')); it('should let you toggle the class', function() { expect(ps.first().getAttribute('class')).not.toMatch(/bold/); expect(ps.first().getAttribute('class')).not.toMatch(/red/); element(by.model('important')).click(); expect(ps.first().getAttribute('class')).toMatch(/bold/); element(by.model('error')).click(); expect(ps.first().getAttribute('class')).toMatch(/red/); }); it('should let you toggle string example', function() { expect(ps.get(1).getAttribute('class')).toBe(''); element(by.model('style')).clear(); element(by.model('style')).sendKeys('red'); expect(ps.get(1).getAttribute('class')).toBe('red'); }); it('array example should have 3 classes', function() { expect(ps.last().getAttribute('class')).toBe(''); element(by.model('style1')).sendKeys('bold'); element(by.model('style2')).sendKeys('strike'); element(by.model('style3')).sendKeys('red'); expect(ps.last().getAttribute('class')).toBe('bold strike red'); });</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-example62/index.html" name="example-example62"></iframe> </div> </div> </p> <h2 id="animations">Animations</h2> <p> The example below demonstrates how to perform animations using ngClass.</p> <p> <div> <a ng-click="openPlunkr('examples/example-example63')" class="btn pull-right"> <i class="glyphicon glyphicon-edit"> </i> Edit in Plunker</a> <div class="runnable-example" path="examples/example-example63" module="ngAnimate" deps="angular-animate.js" animations="true"> <div class="runnable-example-file" name="index.html" language="html" type="html"> <pre><code><input id="setbtn" type="button" value="set" ng-click="myVar='my-class'"> <input id="clearbtn" type="button" value="clear" ng-click="myVar=''"> <br> <span class="base-class" ng-class="myVar">Sample Text</span></code></pre> </div> <div class="runnable-example-file" name="style.css" language="css" type="css"> <pre><code>.base-class { -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s; transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.5s; } .base-class.my-class { color: red; font-size:3em; }</code></pre> </div> <div class="runnable-example-file" name="protractor.js" type="protractor" language="js"> <pre><code>it('should check ng-class', function() { expect(element(by.css('.base-class')).getAttribute('class')).not. toMatch(/my-class/); element(by.id('setbtn')).click(); expect(element(by.css('.base-class')).getAttribute('class')). toMatch(/my-class/); element(by.id('clearbtn')).click(); expect(element(by.css('.base-class')).getAttribute('class')).not. toMatch(/my-class/); });</code></pre> </div> <iframe class="runnable-example-frame" src="examples/example-example63/index.html" name="example-example63"></iframe> </div> </div> </p> <h2 id="ngclass-and-pre-existing-css3-transitions-animations">ngClass and pre-existing CSS3 Transitions/Animations</h2> <p> The ngClass directive still supports CSS3 Transitions/Animations even if they do not follow the ngAnimate CSS naming structure. Upon animation ngAnimate will apply supplementary CSS classes to track the start and end of an animation, but this will not hinder any pre-existing CSS transitions already on the element. To get an idea of what happens during a class-based animation, be sure to view the step by step details of <a href="api/ng/service/$animate#addClass">$animate.addClass</a> and <a href="api/ng/service/$animate#removeClass">$animate.removeClass</a>.</p> </div>