angularjs - 带有自定义指令的 Angular ng-repeat

标签 angularjs angularjs-directive angularjs-ng-repeat directive

我在将 Controller 范围内的数据传递到自定义指令时遇到困难。 ng-repeat 给了我正确的元素数量,它只是没有加载到模板或其他东西中。

angular.module('myApp')
.controller('WorkflowController', ['$scope', function($scope){
    $scope.columns = ['Requested Quote', 'Quote Sent', 'Deposit Paid'];
}])
.directive('kanban-column', function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {column: '='},
    templateUrl: 'directives/kanban-column/template.html'
  }
})

// template.html:
<h4>{{column}}}}</h4>

在我的index.html中使用这个:

<div class='kanban-board'>
    <kanban-column data-ng-repeat="column in columns" data-column="column">
    </kanban-column>
</div>

为了清晰起见,代码被简化了一点,但即使上面的逐字记录也不起作用。我是否忽略了什么?

最佳答案

首先,您尚未在您的应用中传递依赖项,因此它永远不会实例化。

angular.module('myApp',[]).controller('WorkflowController', ['$scope', function($scope){
    $scope.columns = ['Requested Quote', 'Quote Sent', 'Deposit Paid'];
}]).directive('kanbanColumn', function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {column: '='},
    template: '<h4>{{column}}</h4>'
  }
});

试试这个。

<div class='kanban-board'>
    <div data-ng-repeat="column in columns">
       <kanban-column data-column="column">
       </kanban-column>
    </div>
</div>

Working plunkr

关于angularjs - 带有自定义指令的 Angular ng-repeat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491692/

相关文章:

angularjs - 将事件绑定(bind)到链接函数中指令的子元素

javascript - 无法访问父范围的属性,但是我可以通过该属性链看到

angularjs - 根据路由参数确定 Angularjs Controller

javascript - 如果没有选择任何选项,则禁用按钮angularJS

angularjs - AngularJS使用拦截器来处理$ http 404s- promise 未定义错误

javascript - 使用 Angular 指令使用 $scope 属性更新 DOM - 一旦指令运行(无需等待事件)

jquery - 从 Controller 获取 $viewValue

javascript - ng-repeat 中缺少的元素

angularjs - 设置 css 类以动态列出选项 AngularJS

angularjs - NG-OPTIONS 中的硬编码值与 NG-REPEAT 中的动态值