sebelga / ionic-advanced-carousel

Carousel directive for Ionic Frameworks that allows any custom template as an item.
http://sebelga.github.io/ionic-advanced-carousel/
GNU General Public License v2.0
42 stars 18 forks source link

Programmatically select item #7

Closed bknill closed 7 years ago

bknill commented 8 years ago

I can see a method to deselect an item, is there one to select an item? (i.e. I want to automatically select the second item rather than the first)

If not I can see how I'd add it I think - just add a new listener with the id and call selectItem(id) right!?

sebelga commented 8 years ago

Yes that should work.
But if you only want to select the fourth item automatically at start you have the 'selectAtStart' property in the options. You could set it like this: selectAtStart { property : 'id', value:yourArray[3].id }

bknill commented 8 years ago

Thanks.. Actually that might be a better idea. Do you know if this data gets updated when the $scope.$broadcast('a-carousel.arrayupdated', 'carousel-1'); gets' called?

There are 2 situations, when I'm starting and want to pre-select an item, and when this is getting called at a later time because the data updated and the item is then deselected.

sebelga commented 8 years ago

Yes it should work.
Although I just realize that the doc is incomplete. You can send the event 'a-carousel.arrayupdated' with an option object (the same as when initiating the carousel) instead of the carouselId.

Important: you must add a "carouselId" property in the option object

So you can do

var carouselOptions = {
    carouselId : 'carousel-1',
    selectAtStart : {
        property : 'id',
        value: 123
    }
    ... all other options of your carousel
}
$scope.$broadcast('a-carousel.arrayupdated', carouselOptions);