Это моя директива. Я получаю значение консоли («привет») в моей консоли, но я не получаю высоту или ширину DIV, к которому я добавил директиву размерget div width height using angularjs
'use strict';
app.directive('resize', function ($window) {
console.log("hello");
return function (scope, element) {
restrict: 'E'
var w = angular.element($window);
console.log(w);
scope.getWindowDimensions = function() {
return {
'h': w.height(),
'w': w.width()
};
};
scope.$watch(scope.getWindowDimensions, function (newValue, oldValue) {
scope.windowHeight = newValue.h;
scope.windowWidth = newValue.w;
console.log(scope.windowHeight);
console.log(scope.windowWidth);
scope.style = function() {
return {
'height': (newValue.h - 100) + 'px',
'width': (newValue.w - 100) + 'px'
};
};
}, true);
w.bind('resize', function() {
console.log(w.bind())
scope.$apply();
});
}
})
ниже в моем DIV
<div data-ng-show="GameHeaderScreen.Start" class="head" ng-style="style()" id="playid" resize>window.height: {{windowHeight}}
<br />window.width: {{windowWidth}}
Это не так, как вы заявляете директиву. – cgTag
Часы вызывается, когда вызывается 'scope. $ Digest()', и если наблюдаемое значение переменной изменилось. digest не вызывается, когда изменяется размер элемента. Таким образом, вы не можете контролировать изменение ширины в '$ watch' – karaxuna
@ karaxuna45: так что теперь я могу добавить? – shank