Sud*_*kar 4 angular2-directives angular
我不确定如何使用ngStyle最新的beta-12指令.请有人澄清一下.
在角文档的链接plnkr https://angular.io/docs/js/latest/api/common/NgStyle-directive.html是过时的,因为他用alpha版本.
我尝试了这些语法,但似乎没有用.一世
[ngStyle]="{'display': none}"
[style.display]="none"
Run Code Online (Sandbox Code Playgroud)
http://plnkr.co/edit/U9EJuIhFqxY9t2sULMdw
import {Component} from 'angular2/core'
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h2 [ngStyle]="{'display': none}">Hello {{name}}</h2>
<h2 [style.display]="none">Hello {{name}}</h2>
</div>
`,
directives: []
})
export class App {
constructor() {
this.name = 'Angular2'
}
}
Run Code Online (Sandbox Code Playgroud)
Abd*_*yer 11
在这两种情况下,none都应该'none'加上引号.因为您应该为string属性赋值display.none(没有qoutes)将在运行时进行评估并返回undefined,这不是css属性的可接受值display
//our root app component
import {Component} from 'angular2/core'
@Component({
selector: 'my-app',
providers: [],
template: `
<div>
<h2 [ngStyle]="{'display': 'none'}">Hello {{name}}</h2>
<h2 [style.display]="'none'">Hello {{name}}</h2>
</div>
`,
directives: []
})
export class App {
constructor() {
this.name = 'Angular2'
}
}
Run Code Online (Sandbox Code Playgroud)
更新
这是来自NgClass指令的 angular2 docs :
表达式评估的结果根据表达式评估结果的类型进行不同的解释:
串 -所有在一个字符串(空格分隔)中列出的CSS类添加
阵列 -所有的CSS类(数组元素)添加
对象而值被解释为表达式评估为布尔值的每个键对应于一个CSS类名- .如果给定的表达式求值为true,则添加相应的CSS类 - 否则将删除它.
@Component({
selector: 'my-app',
providers: [],
styles:['.hide{display:none}',
'.border{border:3px solid blue}',
'.redBack{background-color:red}'
],
template: `
<div>
<h2 [ngStyle]="{'display': 'none'}">Hello {{name}}</h2>
<h2 [style.display]="'none'">Hello {{name}}</h2>
<h2 [ngClass]="'redBack'">Hello {{name}}</h2> // just normal string
<h2 [ngClass]="{'hide':hideFlag}">Hello {{name}}</h2> // will add class 'hide' if hideFlag is true
<h2 [ngClass]="mulClasses">Hello {{name}}</h2> // will add an array of classes ['border','redBack']
</div>
`,
directives: []
})
export class App {
hideFlag = false;
mulClasses = ['border','redBack']
constructor() {
this.name = 'Angular2'
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10308 次 |
| 最近记录: |