This has been asked before on Stack Overflow, but for some reason I keep getting errors that certain properties are undefined!
So I have the following controller:
phonecatControllers.controller('AboutCtrl', function($scope, $state) {
$scope.startListenToScroll = function($scope, $state) {
$('.subsection').each(function(i) {
var position = $(this).position();
$(this).scrollspy({
min: position.top,
max: position.top + $(this).height(),
onEnter: function(element, position) {
if (element.id) {
$state.transitionTo('about.' + element.id);
} else {
$state.transitionTo('about');
}
}
});
});
}
$scope.startListenToScroll($scope, $state);
$scope.stopListenToScroll = function() {
$('.subsection').unbind().removeData();
}
});
Which has two functions for binding and unbinding a scrollspy plugin to an area within my about page.
I also have a directive which scrolls the user to certain sections on click of links:
.directive('scrollTo', function() {
return {
link: function(scope, element, attrs) {
element.bind('click', function() {
scope.stopListenToScroll();
var divPosition = $('#' + attrs.scrollTo).offset();
$('html, body').animate({
scrollTop: divPosition.top
}, "slow", function() {
scope.startListenToScroll();
});
});
}
};
});
As you can see I call the unbind of the scrollspy on click and then re-bind them after the animation is complete. This is stop the scrollspy plugin listening to the scroll caused by the scrollTo animation.
However I get the error: Uncaught TypeError: Cannot read property 'transitionTo' of undefined presumably because it can't see $state.
You'll note I don't pass anything in the directive, but this is because it should be defaulting in the controller right? If not, how can I handle this?
Any ideas?