0

So this is is an angularjs app.

I have implemented this angular-scroll api :https://github.com/oblador/angular-scroll, to show a catalog of products, where the content is loaded from db. this catalog has all the subcategories (with its products) and every subcategory has an anchor identified like: anchor+categoryId.

So from the menu , i click a category and it scroll nicely to the correct section.

The problem arise when I need to create some links from other pages of the site, to go to an specific section category inside the catalog. Because I have ng-route, i need to create a new url to redirect to the catalog, and there capture when the content is loaded to do the scroll to the required category.

BUT I have a directive associated with the route of the catalog, that looks for the partials depending on the domain of the client, so to show the correct template i have to use an $http , get the content and replace it in my directive.

Because that I dont know how i can know when the content of the directive is ready to make the call to the scroll... better show some code here :

this is the route that is receiving the call

    $routeProvider.
    when('/products/category/:categoryId/page/:page/anchor/:anchorId?', {
        template:'<product-display-view></product-display-view>',
        controller: 'ProductListCtrl',
        access: {
            authorizedRoles: [USER_ROLES.all]
        },
        resolve: {
            wait : 'waitForIt',
            prefetchDataProducts: ['waitForIt','$route','SearchService',
                function(waitForIt,$route,SearchService) {
                    return waitForIt.then(function() {

                        return SearchService.getProducts($route.current.params.categoryId,$route.current.params.page);

                    });
                }],
            prefetchDataCategories:['waitForIt','CategoryService',
                function(waitForIt,CategoryService) {

                    return waitForIt.then(function() {

                        return CategoryService.getCategories();


                    });
                }]
        }
    }).

this is the directive product-display

productDirectives.directive('productDisplayView',['$rootScope','$compile','$http','$templateCache'  ,'$document',
    function($rootScope,$compile, $http, $templateCache,$document){

        return {

            restrict: 'E',
            link: function (scope, element, attrs) {

                var templateUrl = 'users/catwizardAngularCore/app/partials/themes/' + scope.app.theme.themeName + '/partials/product-display.html';

                $http.get(templateUrl, {cache: $templateCache})
                    .success(function (templateContent) {


                        element.replaceWith($compile(templateContent)(scope));


                    });

                /* this doesn't work because the someElement doesn't exist*/
                var newHash = 'anchor' + scope.anchorId;
                var someElement = angular.element(document.getElementById(newHash));
                angular.element(someElement).ready(function () {



                    $document.scrollToElement(someElement, 200, 2000);




                });


            }

        }]);
cjmm
  • 425
  • 4
  • 11

1 Answers1

1

There is a duplicate question with the correct answer, but it has not been accepted yet so I am copying the answer here.

The $anchorScroll has to occur after the page has been rendered, otherwise the anchor doesn't exist. This can be achieved using $timeout().

$timeout(function() {
  $anchorScroll('myAnchor');
});

Credits to Tony

Community
  • 1
  • 1
Brent Washburne
  • 12,904
  • 4
  • 60
  • 82