?
本文檔使用 php中文網(wǎng)手冊 發(fā)布
ngClass
指令允許你動態(tài)設置HTML元素的CSS類,通過綁定到一個包含要添加的所有類的表達式。
這個指令有三種使用方式,這三種方式取決于表達式計算結果:
如果表達式結果為字符串,則字符串為使用空格分隔的一個或多個類名。
如果表達式結果為一個數(shù)組,則數(shù)組中每個元素為使用空格分隔的一個或多個類名字符串。
這個指令不會添加重復的類,如果這個類已經(jīng)存在的話。
當表達式改變時,以前添加的類會被移除,并且只會添加之后新產生的類。
<ANY
ng-class="">
...
</ANY>
<ANY class="ng-class: ;"> ... </ANY>
add - 在類被應用到元素前發(fā)生
remove - 在類從元素移除前發(fā)生
點擊這里 了解更多關于涉及動畫的步驟。參數(shù) | 類型 | 詳述 |
---|---|---|
ngClass | expression | 用于計算的表達式。 計算結果可以為使用空格分隔的類名字符串、數(shù)組,或布爾值的類名映射(map)表。如果是映射表,那些值為真的屬性名稱將作為css類添加到元素上。 |
下面演示使用ngClass指令的一些基本綁定用法。
<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>
style.css.strike {
text-decoration: line-through;}.bold {
font-weight: bold;}.red {
color: red;}
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');});
下面的例子演示使用ngClass來操作動畫。
<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>
.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;}
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/);});
ngClass指令仍然支持CSS3 Transitions/Animations 事件上,如果它們不遵從ngAnimate CSS 命名結構的話。在動畫階段,ngAnimate將用于輔助CSS類來跟蹤動畫的開始和結束。但這并不妨礙任何元素上已經(jīng)存在的CSS轉換。要想了解基于類的動畫過程中發(fā)生了什么,可參見 $animate.addClass 和 $animate.removeClass。