2016-05-17 12 views
1

を属性値を変更する:は、私は、単純なangular2コンポーネント持っangular2テンプレートで

import {Component} from 'angular2/core'; 
@Component({ 
    selector: 'circle', 
    template: `<svg height="100" width="100"> 
    <circle cx="50" cy="50" r="40" stroke="black" 
     stroke-width="3" fill="red" /> 
    </svg>`, 
}) 
export class Circle { } 

をそして私は、動的変更したいfill属性を<circle color="red"></circle>のように、たとえば、コンポーネントのいくつかのパラメータを使用して、結果として赤丸を持っています。出来ますか?

答えて

3

アトリビュートの接頭辞がattr[...]の構文を使用できます。あなたがコンポーネントに入力を提供したい場合は、

@Component({ 
    selector: 'circle', 
    template: `<svg height="100" width="100"> 
    <circle cx="50" cy="50" r="40" stroke="black" 
     stroke-width="3" [attr.fill]="fillAttr" /> 
    </svg>`, 
}) 
export class Circle { 
    fillAttr:string = 'red'; 
} 

@Inputデコレータ使用:以下を参照してください

@Component({ 
    selector: 'circle', 
    (...) 
}) 
export class Circle { 
    @Input('color') 
    fillAttr:string = 'red'; 
} 
+0

おかげで、しかし、どのように私はサークルの要素の属性からfillAttr' 'にデータを渡すことができますか? –

+0

あなたはあなたのコンポーネントを使用するときを意味しますか? –

+0

はい、 'のように ' –

関連する問題