ng-transclude显示元素两次

时间:2015-09-18 00:07:41

标签: javascript angularjs angularjs-ng-transclude

为什么此transcluded指令会重复 Name inside directive = Frank 两次?

我以为我理解了翻译,但这让我很困惑。

Here's a fiddle

<div ng-app="myApp" ng-controller="myController">
    <my-directive>Name in outer controller scope = {{name}}<my-directive/>
</div>


var app = angular.module("myApp", []);
app.controller("myController", function ($scope) {
    $scope.name = 'George';
});
app.directive("myDirective", function () {
    return {
        restrict: 'E',
        scope: {},
        transclude: true,

        template:   '<div ng-transclude></div>' +
                    '<span>Name inside directive = {{name}}</span>',

        link: function (scope) {
            scope.name = 'Frank'
        }
    };
});

2 个答案:

答案 0 :(得分:0)

只是因为你没有正确关闭你的标签

</my-directive>代替<my-directive/>

答案 1 :(得分:0)

这是一个错字:

<my-directive>Name in outer controller scope = {{name}}</my-directive>