我想将父元素的高度加上父元素的margin-top添加到元素的高度。 我使用此指令但没有使用
app.directive('sizetofit', function () {
return {
restrict: "A",
link: function (scope, element) {
scope.getWidth = function () {
return element[0].parentNode.clientWidth;
};
scope.$watch(scope.getWidth, function (width) { element.css('height', (element.parent().css('margin-top')+width/1.5) + 'px');
console.log(element.css('height'));
});
}
}
});

答案 0 :(得分:0)
这很好用。您可以添加代码并进行检查。
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl', ['$scope', function($scope) {
$scope.greeting = 'Arindam!';
}]);
myApp.directive('sizeof', function($timeout, $parse) {
return {
restrict:'A',
link: function(scope, el, attrs) {
console.log($(el).parent());
$(el).parent().css({'height':'200px','margin-top':'20px'});
}
};
})

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div>
<ul sizeof>
<li>{{greeting}}</li>
</ul>
</div>
</div>
&#13;