【发布时间】:2015-05-31 14:21:44
【问题描述】:
我在 AngularJs 1.3 中定义了一个 transclude 指令,但它似乎对渲染没有任何影响。
链接阶段的日志语句显示该指令已被调用。
index.html
<html lang="en" ng-app="directivesApp">
<head>
<script src="js/angular.min.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-controller="directiveCtrl">
<label>Geography</label><input type="text" ng-model="geography"/>
<br>
<div transclude-demo>
<button ng-click='showGeography()'>Show Geography</button>
<a href="#">and a link</a>
</div>
</body>
</html>
app.js
angular.module('directivesApp', [])
.controller('directiveCtrl',function($scope){
$scope.showGeography=function(){alert('I am here');}
})
.directive('transcludeDemo',function(){
return{
transclude: true,
template: '<div ng-transclude> This is my directive content</div>',
link:function ($scope, element, attrs) { console.log('invoked in scope');}
}
});
我本来希望 transclude 指令用其模板的内容替换/修改 div 的内容。
但是,我发现 div 是按原样呈现的。
transclude 指令是这样工作的吗?
【问题讨论】:
-
您应该看到按钮和链接呈现,而不是文本
"This is my directive content"。ng-transclude将其元素的内容替换为嵌入的内容 -
为了更好的测试,将你的指令模板替换为
<div><div ng-transclude></div><div>This is my directive content</div></div>
标签: angularjs angularjs-directive