我正在使用AngularJS指令。這個指令有一些自定義屬性。我將使用該指令是這樣的:綁定指令範圍對HTML的價值
<my-directive show-links="false" />
show-links
是,我想默認爲true
如果開發者沒有明確設置它的屬性。在試圖建立這個指令,我有以下的JavaScript:
.directive('myDirective', function() {
return {
restrict:'E',
transclude: true,
scope: {
showLinks: '=?'
},
templateUrl: '/directives/my-directive.html',
controller: function ($scope) {
if (angular.isUndefined($scope.showLinks)) {
$scope.showLinks = true;
console.log('show links? ' + $scope.showLinks);
}
}
};
});
我的HTML我-directive.html如下所示:
<div><h1>Hello! {{showLinks}}</h1></div>
<div ng-if="showLinks == true"">
<a href='#'>do something</a>
</div>
當該指令進行評估,我可以看到在控制檯窗口中「顯示鏈接?真實」。但是,在屏幕上,我看不到「真實」打印在「你好」旁邊。就像HTML在showLinks處於範圍之前得到渲染一樣。我究竟做錯了什麼?
嘗試使用'compile'而不是'link'。它用於不同的階段。 – denisazevedo 2014-10-06 13:24:46