2013-09-23 22 views
4

我有一个数组。数组中的每一项都包含指令的数据。该数组是在控制器内部创建的。AngularJS指令与示波器数据阵列

示范

$scope.myDirectiveData = 
      [ { title: "First title", content: "First content" }, 
      { title: "Second title", content: "Second content" } ]; 

的指令模板

<div class="MyDirective"> 
    <h1>{{myDirectiveData.title}}</h1> 
    <p>{{myDirectiveData.content}}</p> 
</div> 

我应该如何落实该指令对数组中的任何项目创建一个项目?喜欢的东西...

<MyDirective data-ng-repeat="item in myDirectiveData"></MyDirective> 

回答

4

下面是一个使用指令的示例。它使用ng-repeat为您的范围内的数组中的每个对象调用您的指令。在这个小提琴这是你在找什么。

http://jsfiddle.net/gLRxc/4/

angular.module('test', []) 

.directive('myDirective', function() { 
    var template = '<div class="MyDirective">' + 
     '<h1>{{ data.title }}</h1>' + 
     '<p>{{ data.content }}</p>' + 
     '</div>'; 
    return { 
     template: template, 
     scope: { 
      data: '=' 
     }, 
     link: function (scope, element, attrs) { 

     } 

    }; 
}) 

.controller('TestController', function ($scope) { 

    $scope.myDirectiveData = [ 
      { title: "First title", content: "First content" }, 
      { title: "Second title", content: "Second content" } 
     ]; 


}) 
; 

的Html编辑:吴重复是在同一行上像你的问题状态的指令。

<div ng-app="test" ng-controller="TestController"> 
    <div my-directive data="item" ng-repeat="item in myDirectiveData"></div> 
</div> 
0
<div ng-repeat="item in myDirectiveData"> 
    <h1>{{item.title}}</h1> 
    <p>{{item.content}}</p> 
</div>