2014-07-03 43 views
-1

我有一個使用屬性構建更復雜標記的指令。將指令屬性綁定到模板

這裏是正在使用的指令:

<alerts heading="Alerts {{ count }}"> 
    <ul> 
    <li ng-repeat="alert in alerts">{{ alert.description }}</li> 
    </ul> 
</alerts> 

該指令模板看起來是這樣的:

<div> 
    <h1></h1> 
    <div ng-transclude></div> 
</div> 

我怎樣才能獲得標題屬性爲h1因此,在改變count更新標題?

+0

假設該指令是在''標記,它的屬性作爲鏈接函數的第三個參數(在作用域和元素之後)可用於該指令。 –

回答

0

您可以隔離您的範圍並使用'@'表示法收集要用於模板指令的變量。另請注意,在您的模板中,ng-transclude拼寫錯誤。

Sample Here

例如,

JAVASCRIPT

directive('alerts', function() { 
    return { 
     restrict: 'E', 
     scope: {heading: '@'}, 
     transclude: true, 
     templateUrl: 'alert.html' 
    } 
    }); 

alert.html

<div> 
    <h1 ng-bind="heading"></h1> 
    <div ng-transclude></div> 
</div> 

使用

<alerts heading="Alerts {{alerts.length}}"> 
    <ul> 
    <li ng-repeat="alert in alerts">{{ alert.description }} <button ng-click="remove(index)">remove</button></li> 
    </ul> 
</alerts>