我有$scope
中的數據,根據classNames
的計數範圍,我需要在頁面中創建具有scope
的不同數據的元素。怎麼樣?如何使用不同的數據創建多個`directive`實例
我想添加更多no.of directive
元素,但我只看到一個輸出。並且我無法將$scope
數據傳遞給它。
這樣做的正確方法是什麼?
這裏是我的嘗試:
<div class="wrapper" ng-app="myApp">
<div class="helloWorld" ng-controller="hello">
<ul>
<li ng-repeat="item in items">
{{item.name}}
</li>
</ul>
<hello-world/> //added multiple times
<hello-world/>
<hello-world/>
<hello-world/>
<hello-world/>
</div>
</div>
var app = angular.module('myApp', []);
app.controller('hello', function ($scope) {
$scope.items = [
{name:'name1', className:'green'},
{name:'name2', className:'blue'},
{name:'name3', className:'brown'},
{name:'name4', className:'yellow'},
{name:'name5', className:'red'}
];
});
app.directive('helloWorld', function() {
return {
restrict: 'AE',
replace: 'true',
template: '<h3 class="{item.className}">Hello World!! from color of {{item.className}}</h3>',
scope: {
className: '@'
},
link : function ($scope, elem, attr) {
}
}
});
任何一個能幫助我理解這個概念,並在此創建指令的多個實例?
在此先感謝。
好,如何映射顏色名稱相應的 – 3gwebtrain
你想循環遍歷'hello-world'多次,並將顏色綁定到它們中的每一個? – dfsq
是的,確切地說。以及我想爲每個添加不同的值。每個需要添加一個點擊事件。 – 3gwebtrain