[英]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.