Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html angularjs基于宽度设置div的高度_Html_Css_Angularjs - Fatal编程技术网

Html angularjs基于宽度设置div的高度

Html angularjs基于宽度设置div的高度,html,css,angularjs,Html,Css,Angularjs,我试图根据div的宽度设置其高度,然后应用乘法因子。我在我的代码中使用angularjs,我需要使用类来作为指令的基础 我的html如下所示: <div ng-class="(box.banner) ? 'bannerbox col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-4' : 'cardbox col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-4'" ng-repeat="box in boxes"&

我试图根据div的宽度设置其高度,然后应用乘法因子。我在我的代码中使用angularjs,我需要使用类来作为指令的基础

我的html如下所示:

<div ng-class="(box.banner) ? 'bannerbox col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-4' : 'cardbox col-xs-12 col-sm-12 col-md-6 col-lg-6 col-xl-4'"  ng-repeat="box in boxes">
app.directive('bannerbox', function () {
return {
    restrict: "C",
    link: function (scope, element) {
        element.height(element.width * 1.08571);
    }
}
}))


任何帮助都将不胜感激

您需要注意元素的宽度,因为宽度的值最初为零

调用link函数时,元素的宽度为零

假设您使用的是
angularJs
jQuery

app.directive('bannerbox', function () {
    return {
        restrict: "A",
        link: function (scope, element) {
            scope.getWidth = function () {
                return $(element).width();
            };
            scope.$watch(scope.getWidth, function (width) {
                // Do your work with the element width.
                // Be careful not to change the width of the element or you will create an infinite loop.
                // Set the height of the element.
            });
        }
   }
});

在这里使用JSFIDLE示例:

您的错误/问题是什么?你的指令怎么了?对不起,我忘了添加。高度并没有改变它的自然状态,所以我不认为指令在起作用。我想知道这是否是因为此代码是使用ng-view动态加载的。此外,该类是使用ng类正确设置的,因此这不是问题所在。您是否尝试过我的解决方案?它对你有用吗?嗨,提摩太-我可以看到它在小提琴上工作,但我不能让它与我的代码一起工作。我不明白为什么,但我不认为该指令被触发,尽管它是明确加载的。