我有一个带有 div 的指令,其可见性由其父级设置。期望“show”为 true,但 div 在页面加载时最初仍然不可见。
Directive:
<div ng-show="{{ show }}">
...
</div>
angular.module('myModule').directive('myDiv', [
function () {
return {
scope: {
show: '='
},
transclude: true,
restrict: 'EA',
controller: myController,
controllerAs: 'ctrlr',
templateUrl: 'myDiv.tpl.html'
};
}]);
Parent:
<div myDiv show="ctrlr.shoudlShow()" ng-repeat="item in ctrlr.items">
</div>
shoudlShow() 有可能在页面加载时返回 false,然后又变为 true。难道指令 myDiv 没有获取 shouldShow() 的最新值吗?我需要观察变化或其他什么吗?
最佳答案
ng-show
需要一个表达式而不是插值,并且您遗漏了 controllerAs
别名
更改为
<div ng-show="ctrlr.show">
更好的方法是使用 ng-repeat
或 ng-if
上的过滤器。如果不需要,添加所有这些元素(以及关联的监视)是没有意义的
关于javascript - Angular指令不获取父级设置的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41968273/