0

I just finished building a REST API with FosRestBundle (Symfony3). Now I want to retrieve some datas with AngularJS from that API.

I firstly workedwith mock data. In fact, I retrieved data from my API using Postman by copying and pasting data to a variable in an AngularJS Service within my project. So the mock service looks like this:

angular.module("MyServiceMock", ['angular.filter'])
    .factory("myService", ['$rootScope', '$filter', function ($rootScope, $filter) {

       var results=         {
           "FAVORITES": [
               {
                   "id": 1,
                   "title": "An Update on The Periscope VIP Program8.",
               },
               {
                   "id": 2,
                   "title": "Storyteller Sit-downs: Sasu Siegelbaum9.",
               },
               ...
        ]
        };

        return {
            getFavorites: function () {
                return results;
            }
             ...
        }
    }])

Now I finished, all the tests I wanted, and decided to work directly with the API and not with a mock anymore.

I've done it pretty easily, by just replacing the mock service with an $http service, and it worked super nicely.

But the problem is that, a jQuery plugin that I used is not working anymore, I don't know why !

I changed many times the order of my includes, but the issues remains. The order of my includes files look like this :

        <script type="text/javascript" src="library/jquery/jquery-2.1.4.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.js"></script>


     <script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
     <script type="text/javascript" src="owl-carousel/owl.carousel.min.js"></script>
            <script type="text/javascript" src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
        <script>
            $(document).ready(function() {

                var owlFavoris = $("#owl-favoris");
                var owlRecents = $("#owl-recents");
                var owlAussiConsultes= $("#owl-aussiConsultes");

                myOwlCarousel(owlFavoris);
                myOwlCarousel(owlRecents);
                myOwlCarousel(owlAussiConsultes);

                function myOwlCarousel(owl) {

                    owl.owlCarousel({

                        items: 5,
                        itemsCustom: false,
                        itemsDesktop: [1399, 4],
                        itemsDesktopSmall: [1100, 3],
                        itemsTablet: [930, 2],
                        itemsTabletSmall: false,
                        itemsMobile: [479, 1],
                        singleItem: false,
                        itemsScaleUp: false,
                        slideSpeed: 200,
                        paginationSpeed: 800,
                        rewindSpeed: 1000,
                        autoPlay: true,
                        autoWeight: false,
                        autoHeight: false
                    })
                }


            });
        </script>
       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-sanitize/1.6.1/angular-sanitize.min.js"></script>
        <script src="https://code.angularjs.org/1.5.8/i18n/angular-locale_fr-fr.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.14/angular-filter.js" ></script>
        <script type="text/javascript" src="bower/angular-ui-tinymce/src/tinymce.js"></script>

        <script type="text/javascript" src="js/angular-app/app.js" ></script>
        <script type="text/javascript" src="js/angular-app/MesDirectives.js" ></script>
        <script type="text/javascript" src="js/angular-app/MyServiceHttp.js" ></script>
        <script type="text/javascript" src="js/angular-app/MyServiceMock.js" ></script>

The owl.carousel plugin doesn't work anymore since I made a connection to the rest api. I use it in many places of my code. For exemple, use it here :

<div id="owl-favoris" class="owl-carousel owl-theme">

   <div  ng-repeat="favorite in data.FAVORITES  | orderBy:'-comment[1].nbreVue' track by $index " class="item">
                     ...
    </div>
 </div>

What is the problem ? Please help !

1
  • try to put your Jquery code in a Directive and put alittle timeout before call the jquery mthod inside directive .. ($timeout) Commented Dec 30, 2016 at 9:49

1 Answer 1

0

try to make a directive and call there Jquery maybe also after a little timeout ... something like:

app.directive('carousel', function ($timeout) {
    return {
        restrict: 'AE',

        link: function (scope, element, attrs) {

            $timeout(function () {


                $('#owl-favoris').carousel();


            });
        }

    };
});
Sign up to request clarification or add additional context in comments.

4 Comments

Doesn't work. I set the timeout every second ... $timeout(function () { $('.carousel').carousel(); 1000})...
but doy you have carousel Jquery plugin installed?
how are you using it? ..something like : <div id="owl-favoris" class="owl-carousel owl-theme" carousel >
Yes I'm using like that. And then I create my directive like this : .directive('carousel', function ($timeout) { return { restrict: 'AE', link: function (scope, element, attrs) { $timeout(function () { console.log("ok"); $('#owl-favoris').owlCarousel(); }, 100); } }; }); But the weird thing is even console.log('ok') doesn't work when I reload the page

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.