0

I am currently learning AngularJS and already started a little project, which is basically an older project of mine, done in jQuery.

Everythings fine so far, but the last hours I wrapped my head around this area. In jQuery back then 5 minutes, but I have no idea what's the best way in AngularJS)

Here is my jQuery: A calendar view. It opens and closes days on click. Once a day is clicked it gets the class opened. Also there is the state locked, where I disable to open the day at all.

$('.mod-item').on('click', function(){
        if ($(this).find('.mod-item-day').not('.locked')) {
            if($(this).find('.mod-item-day').hasClass('open')){
                $(this).find('.mod-item-day').removeClass('open').addClass('opened');
            }else{
                $(this).find('.mod-item-day').addClass('open');
            }
        }
});

Here is the markup:

<ul class="mod">
    <li class="mod-item">
        <div class="mod-item-day opened"><span>1</span></div>
        <div class="mod-item-content">
            <img src="../images/present1_late.jpeg" alt="">
        </div>
    </li>

So my question is - what's the most AngularJS way to do it?

2

1 Answer 1

3

You do this basically the way shown in the To Do example on the Angular website.

  1. On the day's div, specify ng-click="callbackInYourScopeCode()". In the To Do example, it's ng-click="archive()"

  2. On the day's div's class, include a property from your model. In the To Do example, for instance, there's <span class="done-{{todo.done}}">{{todo.text}}</span>. Note the property todo.done.

  3. In your scope code, have the function (callbackInYourScopeCode) change your model's property (done in the To Do example).

Angular will call your scope code in response to a click, and then update the element based on changes to your model.

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

2 Comments

Hi, i feel stupid now. I did the tutorial side by side on my own - but never got this abstract thought of using this for this simple task. It worked, and much cleaner and simpler as my first tries!
@flrnz: Don't feel stupid, we've all been there. Glad this helped!

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.