1
我已經在AngularJs 1.3中定義了一條transclude指令 - 但它似乎對渲染沒有任何影響。
鏈接階段的日誌語句顯示指令被調用。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指令如何工作?
你應該看到按鈕和一個鏈接渲染,而不是文本「這是我的指令內容」。 'ng-transclude'用橫越內容替換其元素的內容 –
爲了更好地測試,請用'