I am new to anugular js and working on simple module ie Image Slider.
In this module, I have just change the image on home page. Everything is working fine. But now, I want to use some animation like when display next image then the previous image will be hide with fading effect.
To use animation, I have used angular animate plugin. When I include this plugin into m app, then browser show the error as given below:
TypeError: fn is not a function
at angular.js:16299
at completeOutstandingRequest (angular.js:4924)
at angular.js:5312
My custom Image sliding JS is:
var ps_mobile_app = angular.module('app', ['ngAnimate', 'ngRoute', 'ngCookies']);
ps_mobile_app.directive('slider', function($timeout) {
return {
restrict: 'AE',
replace: true,
scope: {
images: '='
},
link: function(scope, elem, attrs) {
scope.currentIndex = 0;
scope.next = function() {
scope.currentIndex < scope.images.length - 1 ? scope.currentIndex++ : scope.currentIndex = 0;
};
scope.prev = function() {
scope.currentIndex > 0 ? scope.currentIndex-- : scope.currentIndex = scope.images.length - 1;
};
scope.$watch('currentIndex', function() {
scope.images.forEach(function(image) {
image.visible = false;
});
scope.images[scope.currentIndex].visible = true;
});
/* Start: For Automatic slideshow*/
var timer;
var sliderFunc = function() {
timer = $timeout(function() {
scope.next();
timer = $timeout(sliderFunc, 5000);
}, 5000);
};
sliderFunc();
scope.$on('$destroy', function() {
$timeout.cancel(timer);
});
/* End : For Automatic slideshow*/
},
templateUrl: 'shared/image-slider/slider.html'
}
});
My Image Slider module html is:
<div class="slider">
<div class="slide" ng-repeat="image in images" ng-show="image.visible">
<img ng-src="{{image.src}}" />
</div>
<div class="arrows">
<a href="#" ng-click="prev()">
<img src="shared/image-slider/img/left-arrow.png" />
</a>
<a href="#" ng-click="next()">
<img src="shared/image-slider/img/right-arrow.png" />
</a>
</div>
</div>
I am using the above module in m home page:
<slider images="slider_images" />
With above code image slider is working fine without using Angular Animate plugin. But with Angular Animate, I am getting this error. After little analysis I think that it is happening because of timeout function.
Can anyone please help me on this issue? Thanks in Advance
Try
var sliderFunc = function() { timer = $timeout(function() { scope.next; timer = $timeout(sliderFunc, 5000); }, 5000); };
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.