角度指令绑定到元素的高度
Angular directive bind to height of element
我对 Angular 相当陌生,希望能够绑定到元素的高度。 在我目前的情况下,我想将 CSS bottom
绑定到el1
的高度 el2
. 它们不共享一个公共控制器。我该怎么做?
<div id='el1' ng-controller='controller1' style='bottom: {{theHeightOfEl2}}'></div>
<div id='el2' ng-controller='controller2' style='height: 573px;'></div>
我在这里找到了一个看起来很有希望的答案,但看不到如何扩展它以允许我指定要将其绑定到哪个元素。
在一般情况下,我想我要求的是一个指令,将元素 1 上的属性 X 绑定到元素 2 上的属性 Y。
更新
我已经创建了一个指令来执行此操作,但它还没有完全起作用。 它在开始时正确触发,但是当我尝试通过手动更新el2
的 CSS 来测试它时,手表不会触发
m.directive('bindToHeight', function ($window) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var attributes = scope.$eval(attrs['bindToHeight']);
var targetElem = angular.element(document.querySelector(attributes[1]));
// Watch for changes
scope.$watch(function () {
return targetElem.height();
},
function (newValue, oldValue) {
if (newValue != oldValue) {
// Do something ...
console.log('Setting bottom to ' + newValue);
elem.attr('bottom', newValue);
}
});
}
};
});
对于任何寻找答案的人,这是我使用的
用法bind-to-height="[cssProperty, sourceElement]"
:
<div bind-to-height="['bottom','#addressBookQuickLinks']">
法典:
m.directive('bindToHeight', function ($window) {
return {
restrict: 'A',
link: function (scope, elem, attrs) {
var attributes = scope.$eval(attrs['bindToHeight']);
var targetElem = angular.element(document.querySelector(attributes[1]));
// Watch for changes
scope.$watch(function () {
return targetElem.height();
},
function (newValue, oldValue) {
if (newValue != oldValue) {
elem.css(attributes[0], newValue);
}
});
}
};
});
例如,
您可以将控制器包装在额外的顶级控制器中
<div ng-controller="PageController">
<div id='el1' ng-controller='controller1' style='bottom: {{theHeightOfEl2}}'></div>
<div id='el2' ng-controller='controller2' style='height: 573px;'></div>
</div>
并在该控制器中计算和存储元素的高度
另一种创建指令并应用于其中一个的方法,它将从 DOM 中找到元素并将高度应用于该元素
您可以使用这个简单的指令轻松完成。基本上,它只监视该属性值的更改。
app.directive('bindHeight', function ($window) {
return {
link: function(scope, element, attrs){
scope.$watch(attrs.bindHeight, function(value) {
console.log(value);
element.css('height',value + 'px');
});
}
};
});
//$scope.myHeight
<div id="main-canvas" bind-height="myHeight">
我正在使用您所做的修改版本。我添加了一个偏移属性和一个超时,让 ui 渲染事件完成:
m.directive('bindToHeight', function ($window) {
return {
restrict: 'A',
link: function(scope, iElement, iAttrs) {
var attributes = scope.$eval(iAttrs.bindToHeight);
var targetElem = angular.element(document.querySelector(attributes[1]));
var offset = attributes[2]? parseInt(attributes[2]) : 0;
var timeoutId;
// Watch for changes
scope.$watch(function () {
timeoutId && $timeout.cancel(timeoutId);
timeoutId = $timeout(function () {
var total = targetElem.height() + offset;
//remove the px/em etc. from the end before comparing..
if (parseInt(iElement.css(attributes[0]).slice(0, -2)) !== total)
{
iElement.css(attributes[0], total);
}
}, 50, false);
});
}
};
});
相关文章:
- 查找元素高度,包括边距
- 根据元素高度和宽度的百分比变化增加或减少字体大小
- 更改第二次推送时不起作用的元素高度
- 指示中的角度手表元素高度
- 在 HTML/CSS 中更改元素高度与宽度的比较
- 获取元素高度的正确方法(使用所有浏览器?)
- 访问NativeScript中的元素高度
- 如何“;“修复”;显示滚动条时的容器元素高度
- 当元素高度不够时,如何强制滚动
- CSS webkit溢出隐藏和HTML元素高度
- 如果元素高度大于 x 添加类“纵向”,则添加类“横向”
- 如何获取离子对话框中动态加载的元素的元素高度
- AngularJS 指令中的元素高度
- 铬:设置元素高度的脚本是什么
- 哇.js偏移等于元素高度
- 如何获得隐藏元素高度
- jQuery - 如果其他元素可见,则更改元素高度
- 根据元素高度拆分段落的最有效方法
- 检测元素高度变化,如果太小则隐藏
- 根据 jQuery 中多个实例上的另一个元素高度设置高度