1

I usually put jQuery code inside a function and then call it from the controller.

The example below is a slideshow, it's been called from the controller but nothing happens. I put in a few console.log which show up except for one function (changePanel function).

Here is the jsfiddle with the code:

https://jsfiddle.net/codeforme2016/rb1cp2L4/

Here the code:

app.directive('shopProject', function () {
 return {
    restrict: 'E',
    templateUrl: 'projects/shop/shop.html',
    scope: {},
    controller: 'shopCtrl',
    link: function(scope){}
 } 
})

app.controller('shopCtrl', function($scope) {

 function slider() {
    console.log("haaaaaa")
    $(function() {
        console.log("haaaaaa2222")

        var SliderModule = (function() {
            console.log("haaaaaa333")
            var pb = {};
            pb.el = $('#slider');
            pb.items = {
                panels: pb.el.find('.slider-wrapper > li'),
            }

            var SliderInterval,
                currentSlider = 0,
                nextSlider = 1,
                lengthSlider = pb.items.panels.length;


            pb.init = function(settings) {
                console.log("haaaaa4444a")
                this.settings = settings || {
                    duration: 8000
                };
                var items = this.items,
                    lengthPanels = items.panels.length,
                    output = '';


                for (var i = 0; i < lengthPanels; i++) {
                    if (i == 0) {
                        output += '<li class="active"></li>';
                    } else {
                        output += '<li></li>';
                    }
                }

                $('#control-buttons').html(output);

                activateSlider();
                // Eventos para los controles
                $('#control-buttons').on('click', 'li', function(e) {
                    var $this = $(this);
                    if (!(currentSlider === $this.index())) {
                        changePanel($this.index());
                    }
                });

            }


            var activateSlider = function() {
                console.log("haaaaaa5555")
                SliderInterval = setInterval(pb.startSlider, pb.settings.duration);
            }

            pb.startSlider = function() {

                var items = pb.items,
                    controls = $('#control-buttons li');
                // Comprobamos si es el ultimo panel para reiniciar el conteo
                if (nextSlider >= lengthSlider) {
                    nextSlider = 0;
                    currentSlider = lengthSlider - 1;
                }

                  controls.removeClass('active').eq(nextSlider).addClass('active');
                items.panels.eq(currentSlider).fadeOut('slow');
                items.panels.eq(nextSlider).fadeIn('slow');


                currentSlider = nextSlider;
                nextSlider += 1;
            }


            var changePanel = function(id) {
                console.log("haaaaaaughg99999")
                clearInterval(SliderInterval);
                var items = pb.items,
                    controls = $('#control-buttons li');
                // Comprobamos si el ID esta disponible entre los paneles
                if (id >= lengthSlider) {
                    id = 0;
                } else if (id < 0) {
                    id = lengthSlider - 1;
                }

                controls.removeClass('active').eq(id).addClass('active');
                items.panels.eq(currentSlider).fadeOut('slow');
                items.panels.eq(id).fadeIn('slow');


                currentSlider = id;
                nextSlider = id + 1;

                activateSlider();
            }

            return pb;
        }());

        SliderModule.init({
            duration: 4000
        });

    });
 }
 slider();

})

HTML:

<section id="slider" class="container">
  <ul class="slider-wrapper">
    <li class="current-slide">
        <img  src="http://i9.photobucket.com/albums/a88/creaticode/1_zpsc6871490.jpg" title="" alt="">

        <div class="caption">
            <h2 class="slider-title">Diseño web</h2>
            <p>Lorem ipsum</p>
        </div>
    </li>

    <li>
        <img src="http://i9.photobucket.com/albums/a88/creaticode/2_zps6ccd36bd.jpg" title="" alt="">

        <div class="caption">
            <h2 class="slider-title">Diseño Responsive</h2>
            <p>Lorem ipsum </p>
        </div>
    </li>

    <li>
        <img src="http://i9.photobucket.com/albums/a88/creaticode/4_zps611bc9f9.jpg" title="" alt="">

        <div class="caption">
            <h2 class="slider-title">Identidad Corporativa</h2>
            <p>Lorem ipsum </p>
        </div>
    </li>

    <li>
        <img src="http://i9.photobucket.com/albums/a88/creaticode/3_zps70e4fcc5.jpg" title="" alt="">

        <div class="caption">
            <h2 class="slider-title">Desarrollo Web</h2>
            <p>Lorem ipsum</p>
        </div>
    </li>
 </ul>

   <div class="slider-shadow"></div>

  <ul id="control-buttons" class="control-buttons"></ul>
</section>

EDIT: the problem seems to be in how I reference my files and the like because its working if all the files are in the same directory-

js/index.html

<script src="projects/shop/shop.directive.js"></script>

js/home/home.html js/home/home.js

hier I have also included jQuery in controllers + called it from insight which WORKS

js/projects/shop/shop.html js/projects/shop/shop.directive.js

hier i have the shop directive and controller and do the same as in the html.js -call jQuery from insight the controller but here NOTHING works.

I def. correctly included all script tags in index.html because otherwise I wouldn't be able to see the content from the templates.

javascript2016
  • 973
  • 3
  • 17
  • 41
  • Resource paths in demo are not valid – charlietfl Jan 31 '17 at 19:39
  • Seems to [work fine here](http://plnkr.co/edit/u0Q8AcVkZTg3j1MnqZb0?p=preview) . Note that code like this belongs in a directive not in controller – charlietfl Jan 31 '17 at 19:41
  • Generally speaking, you should be using AngularJS for things like attaching event handlers, controlling HTML, etc., not jQuery. It may be easier if you already have created the jQuery previously, but it'll end up confusing you and others down the line. – Heretic Monkey Jan 31 '17 at 20:08
  • I just looked at your link and yes it works-so i guess its about how I referenced things/the file structure I guess. Please see my edit – javascript2016 Jan 31 '17 at 20:13
  • 1
    I use jQuery because i have no idea how to do dom manipulation with angular and making a simple slider would take me forever using just angular, so i don't. – javascript2016 Jan 31 '17 at 20:14
  • Read [“Thinking in AngularJS” if I have a jQuery background?](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background). – georgeawg Jan 31 '17 at 21:20
  • i maybe will but for now I just want to find out why its not working-that be great. – javascript2016 Jan 31 '17 at 22:01

0 Answers0