1

Trying to load the RoyalSlider as a Directive. Here's my directive, which works though I'm not sure exactly why, on first load but not on subsequent loads:

app.directive('royalSlider', ['$timeout', function($timeout) {

    $(".royalSlider").royalSlider({
        keyboardNavEnabled: true,
        arrowsNav: true,
        arrowsNavHideOnTouch: true,
        imageScaleMode: 'fill',
        slidesSpacing: 0
    });

}]);

with the error:

TypeError: Cannot read property 'compile' of undefined

Assuming the issue is loading when all content is finished, I changed it to this:

app.directive('royalSlider', ['$timeout', function($timeout) {
    return {
        link: function ($scope, element, attrs) {
            $scope.$on('$viewContentLoaded', function () {

                $(".royalSlider").royalSlider({
                    keyboardNavEnabled: true,
                    arrowsNav: true,
                    arrowsNavHideOnTouch: true,
                    imageScaleMode: 'fill',
                    slidesSpacing: 0
                });

            })
        }
    }
}]);

And Nothing happens. $timeout is also in there because I've tried that trick too, to no avail.

Gabriel M Sharp
  • 335
  • 1
  • 6
  • 17

2 Answers2

0

Try this

app.directive('royalSlider', ['$timeout', function($timeout) {
    return {
        link: function ($scope, element, attrs) {

            $scope.$apply($(".royalSlider").royalSlider({
                    keyboardNavEnabled: true,
                    arrowsNav: true,
                    arrowsNavHideOnTouch: true,
                    imageScaleMode: 'fill',
                    slidesSpacing: 0
                }));


        }
    }
}]);

Or

app.directive('royalSlider', ['$timeout', function($timeout) {
        return {
            link: function ($scope, element, attrs) {

                $(".royalSlider").royalSlider({
                    keyboardNavEnabled: true,
                    arrowsNav: true,
                    arrowsNavHideOnTouch: true,
                    imageScaleMode: 'fill',
                    slidesSpacing: 0
                });
                 $scope.$apply();


            }
        }
    }]);
Mathews
  • 909
  • 1
  • 7
  • 13
  • Unfortunately neither work, and more unfortunately there's no clue in the console to what's going wrong. – Gabriel M Sharp May 29 '14 at 13:53
  • Ok to give it one more go try this app.directive('royalSlider', ['$timeout', function($timeout) { return { link: function ($scope, element, attrs) { $(".royalSlider").royalSlider({ keyboardNavEnabled: true, arrowsNav: true, arrowsNavHideOnTouch: true, imageScaleMode: 'fill', slidesSpacing: 0 }); $scope.$apply(); } } }]); – Mathews May 29 '14 at 13:57
  • All options are working with the addition of Restrict: "AEC", but there is an error in console. `Error: [$rootScope:inprog] $digest already in progress` – Gabriel M Sharp May 29 '14 at 14:18
  • Ok then the issue was not having the restrict. So the new error is because we use $scope.$apply(). Can you remove $scope.$apply() and see if it is still working without any error? – Mathews May 29 '14 at 14:24
0

I already have 2 situations where directives and services/factories didn't play well.

The scenario is that I have (had) a directive that has dependency injection of a service, and from the directive I ask the service to make an ajax call (with $http).

In the end, in both cases the ng-repeat did not file at all, even when I gave the array an initial value.

I even tried to make a directive with a controller and an isolated-scope. Only when I have moved everything to a controller then it worked like magic. One of them was the royal slider.

Here is my code

app.service('AjaxService', ['$log', '$http', function ($log, $http) {
    var me = this;
    me.CallHttpAjaxAndMapSearch = function (url, targetObjext, propertyName, callback, splitObject) {
        $http.get(url)
               .success(function(data){
                //etc., mapping to selectedArray
                if (propertyName) {
                    targetObjext[propertyName] = selectedArray;
                }
                else {
                    targetObjext['selectedArray'] = selectedArray;
                }
                if (callback) {
                    callback();
                }
            });
    };
}]);

app.service('slidersService', ['$log', '$http', 'AjaxService', function ($log, $http, AjaxService) {
    var me = this;
    me.initRoyalSlider = function (url, element, arrayName, sliderOptions, splitObject) {
        me[arrayName] = [];
        var successCallback = function slidersService_successCallback() {
            setTimeout(function slidersService_successCallback_Timeout() {
                $log.log('setTimeout for ' + arrayName);
                element.royalSlider(sliderOptions);
            }, 0);

        };
        AjaxService.CallHttpAjaxAndMapSearch(url, me, arrayName, successCallback, splitObject);
    };
}]);

app.controller('royalSlider', function ($scope, $attrs, $log, slidersService) {
    var arrName = $attrs.royalSlider;
    var options = JSON.parse($attrs.royalSliderOptions);
    var element = $attrs.$$element;

    $scope.svc = slidersService;
    slidersService.initRoyalSlider($attrs.royalSliderUrl, element, arrName, options, $attrs.splitObject);
});

And the HTML

            <div class="slides royalSlider rsMinW "
                    ng-controller="royalSlider" 
                    royal-slider="mainSlider" 
                    royal-slider-options='{"loop":true,"autoPlay":{"enabled":true,"pauseOnHover":true,"delay":3000},"arrowsNav":false,"controlNavigation":"bullets"}'
                    royal-slider-url="/_api/search/query?bla bla">
                   <div ng-repeat="slide in svc.mainSlider">
Entwickler
  • 255
  • 2
  • 12
bresleveloper
  • 5,940
  • 3
  • 33
  • 47