ng-transclude as:element vs attribute

时间:2014-08-25 15:23:29

标签: javascript angularjs angularjs-directive transclusion angularjs-ng-transclude

我想创建一个包装器指令,该指令将用作列表中通知窗口小部件的框架。在那个框架中,我想稍后根据notif对象的属性转换一些特定内容。目前,我对div元素进行了硬编码。

我在index.cshtml中有以下内容:

<div ng-controller="notificationCenterCtrl">
    <ul>
        <li ng-repeat="notif in allNotifications">
            <notification-base notification="notif" remove="removeNotification(notif)">
                <div style="background-color: fuchsia">bla bla</div> 
            </notification-base>
        </li>
    </ul>
</div>

这是指令规范:

ns.directive("notificationBase", function() {
    return {
        restrict: 'E',
        replace: true,
        transclude: true,
        templateUrl: '/Scripts/notification-base.html',
        controller: 'notificationBaseCtrl',
        scope: {
            notification: '=',
            removeNotif: '&remove'
        }
    };
});

为什么以下工作,即它在紫红色背景中显示被转换的div

<div>
    My notification title: {{notification.name}}
    <div ng-transclude id="notificationContent">

    </div>
    <a ng-click="remove()">Remove</a>
</div>

...但如果我像一个元素一样使用紫红色div不再出现。

<div>
    My notification title: {{notification.name}}
    <div id="notificationContent">
        <ng-transclude></ng-transclude>
    </div>
    <a ng-click="remove()">Remove</a>
</div>

如果我使用ng-transclude作为属性或元素,有什么区别。 (我使用的是Firefox)。

1 个答案:

答案 0 :(得分:2)

仅限最近(因为此pull requestngTransclude指令支持使用element

您可能正在使用角度版本&lt; 1.3 ngTransclude用作element 的{{1}}不支持 - 这是IE8 support的主要原因。