0

I am making a menu with AngularJs and I want that, clicking on the item, the page will scroll to the section of the clicked item. My code is the following:

script.js

var app = angular.module('allApps',['ui.bootstrap']);
app.controller("menuCtrl",function($scope, $location, $anchorScroll){
    $scope.menuItems=[
                      {page:"Biography", id:"bio"},
                      {page:"Curriculum Vitae", id:"cv"},
                      {page:"Gallery", id:"gallery"},
                      {page:"Video", id:"video"},
                      {page:"Press", id:"press"},
                      {page:"News", id:"news"},
                      {page:"Contact", id:"contact"}
                      ];

    $scope.scrollTo = function(id) {
        $location.hash(id);
        console.log($location.hash());
        $anchorScroll();
    };
});

menu.html

<ul class="nav nav-pills" id="mainMenu" ng-controller="menuCtrl">
    <li class="active"><a href="#/">Home</a></li>
    <li ng-repeat="item in menuItems"><a ng-click="scrollTo('{{item.id}}')" href="">{{item.page}}</a>
</ul>

the code like this doesn't work but, if I write explicitly

ng-click="scrollTo('bio')"

it does (obviously it will scroll all the links to biography page). I thought it was a problem in reading AngularJs directions but if I check it with firebug I can see it takes the correct id's.

May you tell me what is wrong?

1 Answer 1

1

You had incorrect expression in ng-click directive, basically it should not contain {{}} interpolation directive in it. The expression which you provide to ng-click will directly evaluate with the context of controller

 ng-click="scrollTo(item.id)" 
Sign up to request clarification or add additional context in comments.

1 Comment

@FabioManniti Glad to know that.. Thanks :-)

Your Answer

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