2016-06-12 23 views
0

內:訪問指令屬性值我創建了包含在HTML如下一條指令模板

<w3-test-directive id="first" url="tableParams" sql="select *from  table1"></w3-test-directive> 

<w3-test-directive id="second" url="tableParams2" sql="select *from  table2"></w3-test-directive> 

現在在我的指令模板,我想屬性id的值。 我怎樣才能得到它?

我的模板是如下:

template: [ 
        '<p>The attribute value is {{value of id attribute}}<p>' 
] 

所以範本看起來有些事情是這樣的:

"<p>The attribute value is first<p>" 

回答

0

可以使用模板函數提供的參數元素和屬性。

template : function(element, attrs){ 
    return '<p>ID is ' + element[0].id +'<p>' 
} 
0

我會想象,一個指令聲明像這樣......

return { 
    restrict: 'E', 
    scope: { 
     id: '@', 
     url: '@', 
     sql: '@' 
    } 
    // and the rest 
} 

...你將能夠直接訪問id因爲它綁定到你的範圍。

template: '<p>The attribute value is {{id}}</p> 
+0

不要認爲模板應該是數組 – charlietfl

+0

@charlietfl:良好的捕獲;我糾正了它。 – Makoto