Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to create a quiz directive and I'm using Angular UI Bootstrap's Carousel directive as a reference.

The Angular UI Boostrap carousel uses an outer 'carousel' directive, plus an inner 'slide' directive. I've looked through the code and the reference implementation, but I'm having trouble understanding something.

The outer markup (i.e. in-page raw, pre-rendered, pre-angularized html) implements an ng-repeat on an array of objects.

<carousel interval="myInterval">
    <slide ng-repeat="slide in slides" active="slide.active">
         <div class="carousel-caption">
             <h4>Slide {{$index}}</h4>
             <p>{{slide.text}}</p>
         </div>
    </slide>
</carousel>

I understand how the repeater works in that "slides" is a reference to a $scope.slides array of objects. What I don't understand is the reference to "slides()" as a function in the template. How does the array become a function?

The directive is implemented as:

.directive('carousel', [function() {
  return {
    restrict: 'EA',
    transclude: true,
    replace: true,
    controller: 'CarouselController',
    require: 'carousel',
    templateUrl: 'template/carousel/carousel.html',
    scope: {
      interval: '=',
      noTransition: '=',
      noPause: '='
    }
  };
}])

And the template at carousel.html is below. Notice the reference to "slides()" in the li repeater.

angular.module("template/carousel/carousel.html", []).run(["$templateCache", function($templateCache) {
  $templateCache.put("template/carousel/carousel.html",
    "<div ng-mouseenter=\"pause()\" ng-mouseleave=\"play()\" class=\"carousel\">\n" +
    "    <ol class=\"carousel-indicators\" ng-show=\"slides().length > 1\">\n" +
    "        <li ng-repeat=\"slide in slides()\" ng-class=\"{active: isActive(slide)}\" ng-click=\"select(slide)\"></li>\n" +
    "    </ol>\n" +
    "    <div class=\"carousel-inner\" ng-transclude></div>\n" +
    "    <a class=\"left carousel-control\" ng-click=\"prev()\" ng-show=\"slides().length > 1\"><span class=\"icon-prev\"></span></a>\n" +
    "    <a class=\"right carousel-control\" ng-click=\"next()\" ng-show=\"slides().length > 1\"><span class=\"icon-next\"></span></a>\n" +
    "</div>\n" +
    "");
}]);

Update

I just looked at the code in the CarouselController and saw these lines. So, that's where that came from. Thanks, pkozlowski.opensource for the confirmation. :^)

var self = this,
    slides = self.slides = [],
    ....;

$scope.slides = function() {
    return slides;
};
share|improve this question

1 Answer 1

up vote 3 down vote accepted

There is no magic here :-) The slides() function is explicitly exposed on a scope by the controller of the carousel directive:

https://github.com/angular-ui/bootstrap/blob/6bc6634cf5dca91331b4147b29f0d0d28fe145d2/src/carousel/carousel.js#L107

share|improve this answer
    
Yeah, I just found it, actually. :^) I was just about to delete the message. BTW, I just finished your book. Great stuff! –  Scott Jan 25 at 18:03
    
Thnx @Scott for the warm words, much appreciated! –  pkozlowski.opensource Jan 25 at 18:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.