When do we know the actual width of an element in Angular?

前端 未结 4 1310
梦如初夏
梦如初夏 2021-01-31 17:22

I\'m tring to create a directive that will center a div.

So far, I have this code:

app.directive(\"setcenter\", function () {
    return {
        scope:         


        
4条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 18:18

    James' answer led me to:

    app.directive('measureInto', function () {
            return {
                restrict: 'A',
                link: function (scope, element, attrs) {
                    scope.$watch(function() {
                        return element[0].clientWidth;
                    }, function(value){
                        scope[attrs.measureInto] = element[0].clientWidth + 10;
                    });
                }
            };
        });
    

    So, at runtime, I add this and assign into whatever scope variable I want the width of the element I'm looking for

提交回复
热议问题