0

We are implementing reusable code in our application for this purpose we have created a simple directive which displays content.

Directive Code:

angular.module("newsStore.moduleDirectives", [])
    .directive('renderAboutContent', ['aboutService', renderAboutContent]);

function renderAboutContent(aboutService) {
    return {
        restrict: 'AE',
        scope: {},
        templateUrl: 'templates/about.html',
        link: function (scope, element) {
            aboutService.getAboutContent()
                .then(function (results) {
                    scope.aboutList = results.aboutContent.listItems;
                }, function (error) {
                    console.log('controller error', error);
                });
        }
    }
}

HTML code:

<div class="col-md-12 aboutUs-ph padT10 content-ph_abt" ng-repeat="content in aboutList">
    <div class="col-md-2 form-group" ng-if="content.image">
        <img src="{{content.image}}" class="img-responsive" />
    </div>
    <div class="col-md-9">
        <span class="guidedTourText"><b>{{content.title}}</b></span>
        <br>
        <span>{{content.description}}</span>
    </div>
</div>

Question:

In the above HTML you can see col-md-2 and col-md-9 inside col-md-12, we want the col-md-2 and col-md-9 width come up as dynamic as sometimes image may not be present in the col-md-2 so in that case text should occupy col-md-12 not col-md-9.

Thanks guys for the answers, this solutions works in this scenario, but one question let's say I have three div elements, and three of them occupies 4, 4 ,4 if the content is present. Let's say if the content is not present in the last div then the remaining two div's should take 6,6 and vice versa. We want this to be implemented from the directive rather than from the html.

Let me know if I am not clear.

7
  • 1
    this is where you might find ng-class to be useful. Commented Nov 6, 2015 at 3:42
  • Can you tell me how to use ng-class in this instance? Commented Nov 6, 2015 at 4:05
  • you are not clear, is this should work in your present exemple, or this a totally different scenario ? Commented Nov 6, 2015 at 4:37
  • 1
    Agreed, OP is vampiring the original question Commented Nov 6, 2015 at 4:38
  • I did say that the solution which you have provided works in the current scenario, I am asking about another case where we might three different div elements. Commented Nov 6, 2015 at 4:38

1 Answer 1

2

Using ngClass may be leveraged here to solve this. Observe the following...

ng-class="{'col-md-12': !content.image, 'col-md-9': !!content.image}"

For some additional reading, this popular blog post is a great reference for understanding the usages offered by this directive: The Many Ways To Use ngClass

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @thomas this one works in this scenario, but one question let's say I have three div elements, and three of them occupies 4, 4 ,4 if the content is present. Let's say if the content is not present in the last div then the remaining two div's should take 6,6 and vice versa.
so you use exactly the same method, ng-if combined to conditionnal ng-class
I didn't get what your trying to say, can you expain that in your answer?
as a minor enhancement/alternative you can represent this in a ternary style as ng-class="content.image ? 'col-md-9' : 'col-md-12'"
not understanding the downvote cast here. This does solve the original question. Perhaps including some basic explanation and a resource could prevent such votes

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.