【发布时间】:2016-02-22 16:26:43
【问题描述】:
我有一个自定义 angular 指令,其后代 div(不是子级,而是层次结构中的某个位置)具有绝对定位。
我想将绝对元素的宽度设置为指令的根宽度,我尝试在指令的链接函数中这样做。
由于各种设计原因,我无法将指令根的显示设置为相对的。
你会如何建议我实现这样的壮举?
HTML
<body ng-controller="TestController">
<my-dir> </my-dir>
</body>
CSS
.my-dir{
width:400px;
height:100px;
background-color:red;
}
.my-dir-content{
position:absolute;
height:150px;
width:100px;
background-color:yellow;
}
脚本
var myApp = angular.module('myApp',[]);
myApp.controller('TestController', ['$scope', function($scope) {
$scope.selectOption = '2';
}]);
myApp.directive('myDir', function() {
return {
restrict: 'E',
link : function (scope, element) {
var myWidth = element.width();
var mySon = element.find('.my-dir-content');
console.log(myWidth);
mySon.width(myWidth); },
template: '<div class="my-dir">' +
'<div class="my-dir-content"> </div>' +
'</div>'
};
});
更新:这是JsBin
【问题讨论】:
标签: javascript jquery html css angularjs