[英]My directive in angular not working
我是新手。
每当文档宽度更改时,我都希望有一个侦听器。
所以首先我决定添加一个$ watch像这样:
$scope.$watch(function(){
return $window.innetWidth;
}, function(n, o){
console.log(n);
console.log(o);
});
但是它仅适用于页面加载,而不适用于调整大小。
然后,我决定编写这样的指令:
app.directive('watchWidth', [function(){
return {
restrict: 'A',
link: function(scope, element) {
element.style.display = 'none';
$scope.$watch(function () {
return element.innerWidth;
}, function (n,o) {
console.log(n);
console.log(o);
});
}
};
}]);
并将指令添加到我的ng-view中:
<div watchWidth ng-view class="main-wrapper"></div>
但是没有任何效果,控制台也没有错误,我也不知道该怎么办!
有什么帮助吗?
嗨,这是我调整特定元素大小的方法
.directive('resize', function (Ls) {
return function (scope, element, attr) {
scope.resizeWithOffset = function (offsetH) {
return {
'min-height': (window.innerHeight - offsetH) + 'px',
'max-height': (window.innerHeight - offsetH) + 'px'
};
}
}
})
在您的html中,它看起来像这样
<ion-scroll ng-style="resizeWithOffset(270)" resize></ion-scroll>
现在基本上可以将元素的大小调整为window-height-您可以指定的高度(在这种情况下为270px)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.