如何将指令范围内的变量绑定到transcluded模板?
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>
答案 0 :(得分: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