Over a million developers have joined DZone.

A better Slideshow App, using Angular

DZone's Guide to

A better Slideshow App, using Angular

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

My colleague Stephane Bisson has a better app than the one ThoughtWorkers and friends made a month ago. The source for Stephane’s project ‘prez’ is on GitHub and the running app is here though you should hit the left or right arrow to get it working. It does not yet do Mobile device swipe left/right. He says he’ll “accept pull requests” to get it that working.

Trying to do the same with Angular

What Stephane did in HTML is invent a element ‘slide’ that he uses like a div. Yes that’s a non-standard element – don’t try it on older browsers. Indeed the modern browsers process this like a div, with CSS transparently adding styling. Angular, via directive can be told to intercept the <slide> element and do the right thing with it – namely show one slide at a time.

Whereas the app we made a month ago, has some repeat-yourself attributes per slide:

<!-- these two rows repeat per slide -->
<tr id="2" ng-show="currentSlide == 2">
        <img ngm-swiperight="nav(1)" ngm-swipeleft="nav(3)" 
              ng-style="imgStyle" src="angular_slide_2.svg"/>
<tr ng-show="currentSlide == 2 && !isTouchScreen" style="display:none;">
        <div class="navitems">
            <a ng-click="nav(1)">back</a>
            <a class="right" ng-click="nav(3)">next</a>

Stephane’s slideshow does not have boiler-plate slide html that the above has. See here:

    <p>This slide shows the dsl. The deck and slide tags.</p>

Stephane’s handles left-arrow-key and right-arrow key without having to encode that per slide, and also the implicit show/hide logic per slide. All that is in the code behind the directive that underpins <slide> from Angular’s point of view. Much less HTML.

Under the hood.

Inside the main app.js script, there’s a directive:

angularModule.directive('slide', function (deck) {
    return {
        restrict: 'E',
        link: function ($scope, $element) {

There’s much more to the app’s JavaScript around ‘deck’ and next/back processing that is shown in that snippet. That makes for more JavaScript than our example from a month ago. But the result is much terser and more elegant HTML.

Put another way, Stephane’s example has less view logic, more controller logic and about the same amount of model properties (referring to MVC of course). With four slides it’s a wash as to while os more code. With 50 slides Stephane’s wins by a wide margin.

I’m reminded of something.

Sami Lempinen coded a simple slideshow technology ‘XSLies’ some 14 years ago using XML and XSL. All that was done on the server side, and the resulting multi-page HTML was shipped to the client side. It was cool stuff in it’s day, and the terse markup language was much the same. Sadly Sami has not been a good historian for his own URLs, so here’s what’s left of it on a third-party site

Making the older slideshow terser too.

Andrew Dean and a few at the Dallas Geeknight meetup went on to make some changes to make that version a bit more DRY too. Less boilerplate via a single navigation row that adapts to the ‘current’ slide:

<tr ng-show="!isTouchScreen" style="display: none;">
        <div class="navitems">
            <a ng-click="nav(currentSlide - 1)" 
               ng-show="currentSlide > 1">back</a>
            <a class="right" ng-click="nav(currentSlide + 1)" 
               ng-show="currentSlide < 4">next</a></div>

Here is their fork with their improvements.

How does Google see it ?

Specifically in the search results, and the preview-panel?

In the search results:

The preview bot’s screen shot:

Not quite what we were hoping for :(

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.


Published at DZone with permission of Paul Hammant, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}