Class toggle

59 views
Skip to first unread message

Олег Истомин

unread,
May 21, 2013, 5:07:51 PM5/21/13
to ang...@googlegroups.com
I made simple toggle http://jsfiddle.net/tamtakoe/waXKy/
But, I don't like code

ng-click="state=(['classA','classB','classC'][(+index<2&&(index=1+index)||(index=0))])"
 
+ we have initial state with no class (must be 'classA')

Maybe there is a better way? Or make, please, function-iterator in angular expressions, f.e. ['classA','classB','classC'][next(index)] (index=0 - start position)

Олег Истомин

unread,
May 21, 2013, 5:25:19 PM5/21/13
to ang...@googlegroups.com
If sombody need complete solution: http://jsfiddle.net/tamtakoe/waXKy/1/
But it is more messy :-(

среда, 22 мая 2013 г., 0:07:51 UTC+3 пользователь Олег Истомин написал:

Raymond Z.

unread,
May 21, 2013, 5:43:23 PM5/21/13
to ang...@googlegroups.com
Here's a slightly cleaner solution: http://jsfiddle.net/razh/SSHYK/.

Олег Истомин

unread,
May 21, 2013, 5:56:40 PM5/21/13
to ang...@googlegroups.com
Cool! Thank!

среда, 22 мая 2013 г., 0:43:23 UTC+3 пользователь Raymond Z. написал:

Олег Истомин

unread,
May 22, 2013, 9:02:53 PM5/22/13
to ang...@googlegroups.com
Some problem with updating: http://jsfiddle.net/tamtakoe/Hnp53/
I can't see new value in $watch (see console.log)


среда, 22 мая 2013 г., 0:43:23 UTC+3 пользователь Raymond Z. написал:
Here's a slightly cleaner solution: http://jsfiddle.net/razh/SSHYK/.

Raymond Z.

unread,
May 22, 2013, 10:07:45 PM5/22/13
to ang...@googlegroups.com
You need to add true as the third argument in $scope.$watch. This makes Angular use object-equality to check if a value has changed (see documentation).

Олег Истомин

unread,
May 23, 2013, 5:54:18 AM5/23/13
to ang...@googlegroups.com
Thank! I get it. objectEquality=true use recursive deep watching (into object).

четверг, 23 мая 2013 г., 5:07:45 UTC+3 пользователь Raymond Z. написал:

Олег Истомин

unread,
May 29, 2013, 12:32:55 AM5/29/13
to ang...@googlegroups.com
I made better toggle, without code in attributes: http://plnkr.co/edit/mPIgUuA9KjnhY3qOpv7A?p=preview


среда, 22 мая 2013 г., 0:07:51 UTC+3 пользователь Олег Истомин написал:
I made simple toggle http://jsfiddle.net/tamtakoe/waXKy/
Reply all
Reply to author
Forward
0 new messages