简体   繁体   English

使用ng-style将动态创建的CSS应用于组件

[英]Applying dynamically created CSS to a component using ng-style

I have an angularjs component to create stylized pictures, but the style is not correctly applied the first time I visit a page (refreshing the pages shows the correct style). 我有一个angularjs组件来创建风格化的图片,但是在我第一次访问页面时,样式没有正确应用(刷新页面显示正确的样式)。 The CSS that is applied is generated dynamically based on the height and width properties of the image being styled. 根据要设置样式的图像的高度和宽度属性,动态生成所应用的CSS。

Can anybody tell me why the CSS is applied on reload but not on the original load, and how to fix it so the CSS is applied the first time the page is loaded? 谁能告诉我为什么将CSS应用于重新加载而不是原始加载,以及如何解决该问题,以便在首次加载页面时应用CSS? Thanks! 谢谢!

In HTML I create a stylized picture: 在HTML中,我创建了一个风格化的图片:

<stylized-picture image-source="someImage.jpg"></stylized-picture>

The stylizedPicture component : stylizedPicture组件

.component('stylizedPicture', {
    templateUrl: 'src/components/stylized-picture.template.html',
    bindings: {
      imageSource: '@imageSource',
    },
    controller: 'StylizedPictureController as stylizedPictureController'
});

stylized-picture.template.html : stylized-picture.template.html

<div ng-style="stylizedPictureController.outerCSS">
    <div ng-style="stylizedPictureController.innerCSS"></div>
       <div ng-style="stylizedPictureController.imgDivCSS">
           <img ng-src="{{stylizedPictureController.imageSource}}">
       </div>
    </div>
</div>

stylizedPictureController : The CSS is based on the image width/height. stylizedPictureController :CSS基于图像的宽度/高度。

.controller('StylizedPictureController', StylizedPictureController);

StylizedPictureController.$inject = ['$scope'];
function StylizedPictureController($scope) {
    var ctrl = this;

    ctrl.$onChanges = function() {
      var img = new Image();
      img.addEventListener("load", function() {
        ctrl.imgWidth = img.naturalWidth;
        ctrl.imgHeight = img.naturalHeight;

        // Fancy stuff to stylize image based on img h/w removed
        // simplified for example
        var paddingBottom = 100;
        ctrl.paddingBottom = paddingBottom;
        ctrl.outerCSS = {"padding-bottom: " + paddingBottom + "%"};
        ctrl.innerCSS = {"padding-bottom: " + paddingBottom + "%"};
        ctrl.imgDivCSS = {"padding-bottom: " + paddingBottom + "%"};
      });
      img.src = ctrl.imageSource;
    };
};

I have tried using the image.onLoad() method instead of the ctrl.$onChanges funtion/ img eventListener, but have the same results. 我尝试使用image.onLoad()方法代替ctrl.$onChanges函数/ img eventListener,但是结果相同。 I have also tried setting ng-style inline, eg ng-style="{'padding-bottom': stylizedPictureController.paddingBottom}" but it doesn't make a difference. 我也尝试过设置ng-style内联,例如ng-style="{'padding-bottom': stylizedPictureController.paddingBottom}"但没有任何区别。

I have found a solution, and thought I'd post it here in case anybody else has this problem in the future. 我找到了一个解决方案,并认为我会在此处发布,以防将来其他人遇到此问题。

Adding: 添加:

$scope.$apply()

to the end of the image load EventListener, after the CSS has been generated and stored on the controller, fixes the issue. 生成CSS并将其存储在控制器上之后,到图像加载EventListener的末尾即可解决此问题。 I believe this is happening because the image load is outside of the angularjs scope, and so when it finishes, angular doesn't know anything has changed and therefore doesn't update the ng-style binding. 我相信发生这种情况是因为图像加载不在angularjs范围内,所以当它完成时,angular不知道任何更改,因此不会更新ng样式绑定。 Forcing it to update the bindings with $scope.$apply() updates the ng-style. 强制它使用$ scope来更新绑定。$ apply()会更新ng样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM