2

Hello I have create an angular directive to add a bakcground and make it width equals to height(1:1) square div

app.directive('backImg', function($timeout){
return function(scope, element, attrs){
    $timeout(function(){
            var url = attrs.backImg;
            console.log(attrs);
            console.log("after printing attrs");
            if (url == null || url == ""){
                    url="/web/header.png";
            }
            element.css({
                    'height' :  element[0].offsetWidth,
                    'margin-bottom' : '20px',
                    'background-image': 'url(' + url +')',
                    'background-size' : 'cover',
                    'background-repeat': 'no-repeat',
                    'background-position': 'center center',
            });
    });
    };
});

This directive is working fine with the elements that appears when the page is first time loaded

....SOME HTML

 <div back-img="{{group.images[0].path}}"> </div> ---> Working fine

....MORE HTML

This time is not working, the only diference is that it been show if sessionSelected is true.

<div class="row" id="active-session" ng-show="userCtrl.sessionSelected">
    <div back-img="{{group.images[0].path}}"> </div> ---> Not working Working
</div>

Can you please help me figure this out, please

EDIT I looks like the directive is been called while it is not showed (ng-show) and for that reason group.images[0].path is null.

how can I do to call directive when visible??. I think it will be the way

Here you can see that the element is been populated with the correct img path when displayed

FIREFOX DEBUG

<div back-img="/web/uploads/56f7f53a2b82bbf75033559a/nKj0DMZrVt.jpeg"></div>

1 Answer 1

1

you are created the <div back-img="{{group.images[0].path}}"> and use the element[0].offsetWidth when the element is on display:none css (because of the ng-show=true attribute). so in order for this to work you need to change the ng-show to ng-if. that way when you change the flag to true it draw the html all over again and then this will call to the link function in the directive

EDIT

you can always use the $watch function that check for update in the url and when it occurs trigger the changes

app.directive('backImg', function($timeout){
  return function(scope, element, attrs){
    scope.$watch(attrs.backImg,function(){
            var url = attrs.backImg;
            console.log(attrs);
            console.log("after printing attrs");
            if (url == null || url == ""){
                    url="/web/header.png";
            }
            element.css({
                    'height' :  element[0].offsetWidth,
                    'margin-bottom' : '20px',
                    'background-image': 'url(' + url +')',
                    'background-size' : 'cover',
                    'background-repeat': 'no-repeat',
                    'background-position': 'center center',
            })
        })
    };
});
Sign up to request clarification or add additional context in comments.

10 Comments

The element actually is showed but is showed the image I have set if url is null if (url == null || url == ""){ url="/web/header.png"; <- That is the image that is been show on the div } the only thing is failling is that the div has the wrong image.
you need to see if that argument is set in the proper way. you can do it by debugging your code on that line and understand it better. it this isn't helping you can always add you code here (the controller function)
I looks like the directive is been called while it is not showed (ng-show) and for that reason group.images[0].path is null. how can I do to call directive when visible??
The arguments is been set when element is displayed ... se question edits
you may want to use the $watch function on the attrs.backImg
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.