0

I want to dynamically change a html class to an <li> element that I am getting over, but class name could not change. The code is here:

<div id="content">
    <ul id="container" ng-controller="ContainerController" ng-init="init()">
        <li ng-repeat="mainEvent in mainEvents" class="element home {{mainEvents.category}}" >
            <div ng-repeat="event in mainEvent.events">
            <a href="{{event.link}}" title="">
                <div class="images"><img ng-src="{{event.url}}"  alt=""/>

                    <div class="title">
                        <div class="title-wrap"><h3><span>{{event.title}}</span></h3></div>
                    </div>
                    <div class="subtitle">
                        <div class="subtitle-wrap"><p><span><span class="line-through">{{event.short_text}}</span></span>
                        </p></div>
                    </div>
                </div>
            </a>
            </div>
        </li>
    </ul>
</div>

Also my JSON object like this:

"main_events": [

    {
        "category": "poland",
        "header": "Kasım 2014",
        "events": [

            {
                "title": "Beyaz Peynir",
                "url": "images/pic05.jpg",
                "short_text": "40.00 TL ",

                "tags": [
                    {
                        "name": "beacon"
                    },
                    {
                        "name": "gps"
                    },
                    {
                        "name": "navigasyon"
                    }
                ],
                "link": "blog-post-ic-mekan-konum-belirleme-sistemi.html"
            }

        ]
    },

I want to change my html class name dynamically according to category value. However, I always see class name like class = "element home", but i want to see class="element home poland". I know I may do this for a boolean or another method, but I don't understand that problem. It should be worked I supposed. So, How can solve this problem ? Thank you.

2 Answers 2

3

Since you are repeating mainEvent use {{mainEvent.category}} insteaed of {{mainEvents.category}}

Change class="element home {{mainEvents.category}}" to class="element home {{mainEvent.category}}"

Since {{mainEvents.category}} is an array, you cannot use this directly

<li ng-repeat="mainEvent in mainEvents" class="element home {{mainEvent.category}}" >

Also you can use the ng-class syntax:

There also you should take, mainEvent.category

 <li ng-repeat="mainEvent in mainEvents" class="element home"  ng-class="mainEvent.category" >
Sign up to request clarification or add additional context in comments.

5 Comments

@Svravan i have tried this but i have seen again only class="element home " :(
Now, i tried again with ng-class syntax, i saw this when i inspect on chrome: "<li ng-repeat="mainEvent in mainEvents" class="element home ng-scope" ng-class="mainEvent.category">" i don't understand
@AlperGuven make sure that mainEvent.category should not be undefined.
I think the variable you have taken is main_events not mainEvents please check that
@Alper Guven, have you checked the variable name?
0

Change class to ng-class:

Snippet :

Change :

<li ng-repeat="mainEvent in mainEvents" class="element home {{mainEvents.category}}" >

This should also work, if you change class="element home {{mainEvent.category}}"

To:

<li ng-repeat="mainEvent in mainEvents" class="element home" ng-class="mainEvent.category">

Comments

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.