javascript - 来自指令的数据未在 ng-repeat 中显示

标签 javascript angularjs angularjs-directive

我已将此问题分解为最简单的形式。基本上我有一个指令,对于演示,还没有真正做任何事情。我有一个将指令作为属性的 div。 div 中来自对象数组的值不会显示。如果我从 div 中删除指令,它们将显示为正常。我显然在这里遗漏了一些非常明显的东西,因为我之前没有任何问题地做过。

这是 Plunk:http://plnkr.co/edit/ZUXD4qW5hXvB7y9RG6sB?p=preview

脚本:

app.controller('MainCtrl', function($scope) {

  $scope.tooltips = [{"id":1,"warn":true},{"id":2,"warn":false},{"id":3,"warn":true},{"id":4,"warn":true}];

});

app.directive("cmTooltip", function () {
    return {
        scope: {
            cmTooltip: "="
        }
    };
});

HTML

<div ng-repeat="tip in tooltips" class="titlecell" cm-tooltip="true">
    A div element: {{ tip.id }}
</div>
<br><br>

Just to prove it works without the directive:
<div ng-repeat="tip in tooltips" class="titlecell">
    A div element: {{ tip.id }}
</div>

最佳答案

有一个 hack 可以通过使用 transclusion 使其在早期版本的 angular 中工作,就像这样:

app.directive("cmTooltip", function () {
    return {
        scope: {
           cmTooltip: "="
        },
        transclude:  true,
        template : '<div ng-transclude></div>'
    };
});

PLNKR

关于javascript - 来自指令的数据未在 ng-repeat 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20610172/

相关文章:

javascript - Angular : Correct way to send parameters to directive

javascript - ngSelect 元素上具有隔离范围属性的指令,用于预选择选项 : It randomly loses value

javascript - 在开发者控制台的“网络”选项卡上访问或获取文件

javascript - AngularJS - 指令事件和 DOM 渲染

javascript - 如何组合 2 个 JavaScript 代码块

javascript - 我如何整理 ng-class?

javascript - HTML/JS : Confused about method to create an image button

angularjs - ng-options 在另一个下拉列表中按值过滤

css - 使用angularjs单击提交按钮后如何在无效输入框中显示红色边框

angular - 延迟加载导致 'ngModel' 找不到问题