javascript - Angular 指令查找父元素 margin-top

标签 javascript html css angularjs

我想将父元素的高度加上父元素的 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/

相关文章:

javascript - 转义 HTML 值是什么意思?

javascript - 为什么Python的datetime和JS的Date不匹配?

javascript - 我可以使用 javascript 在本地创建和保存一个巨大的文件吗?

javascript - block 代码预览

javascript - AngularJs 总是将最后一个类应用于 body 标签

jquery - 为什么我在浏览器 Firefox 中的占位符后没有红星?

javascript - 是否有类似于 iOS 的 canOpenURL 的东西来检查移动浏览器的 URL 方案?

html - 谷歌地图(现代)集成

javascript - Angular 表达式的安全性

css - Bootstrap 4 中的响应图像