ng-transclude as: element vs attribute

倾然丶 夕夏残阳落幕 提交于 2019-12-20 02:26:46

问题


I want to create a wrapper directive that would serve as the frame for a notification widget in a list. In that frame I want to transclude later some specific content based on a property from the notif object. Currently I hard coded a div element.

I have the following in 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>

This is the directive spec:

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

Why does the following work, that is it displays the transcluded div in fuchsia background?

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

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

...but if i use it like an element the fuchsia div no longer shows up.

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

What is the difference if I use ng-transclude as an attribute or an element. (I use Firefox).


回答1:


Only recently (since this pull request) ngTransclude directive supports usage as element.

You are probably using angular with version < 1.3 where ngTransclude used as element is not supported - the primary reason for this being IE8 support.



来源:https://stackoverflow.com/questions/25489225/ng-transclude-as-element-vs-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!