如何将变量绑定到传输部件中

3
如何将指令作用域中的变量绑定到传输模板中?
app.directive('foo', function(){
    return {
        restrict: 'E',
        transclude: true,
        template: '<div ng-transclude></div>',
        link: function (scope) {
            scope.num = 5;
        }
    }
})

<div ng-app="app">
    <foo>
        {{num}}
    </foo>
</div>
1个回答

2

你缺少一个应用程序模块。我还添加了一个modified类,以便您可以看到模板正在被应用:

var app = angular.module("app", []);

app.directive('foo', function(){
    return {
        restrict: 'E',
        transclude: true,
        template: '<div class="modified" ng-transclude></div>',
        link: function (scope) {
            scope.num = 5;
        }
    }
});

查看plnkr: http://plnkr.co/edit/x9NE6A4kkqspKbO08yhq?p=preview


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接