我想将父元素的高度加上父元素的 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'));
});
}
}
});
最佳答案
This is working fine. You can add in your code and check.
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>
关于javascript - Angular 指令查找父元素 margin-top,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38629275/