所有书签都重定向到相同的元素

时间:2017-09-03 10:57:55

标签: angularjs ctrl

我有三个书签,两个在同一页面上,一个在不同的页面上,当我点击链接时,它移动到相同的元素并且它是平滑的。
我使用的框架工作是materializecss,angularjs 1和ui router
我不知道如何为它编写代码。



myapp.controller('ctrl',ctrl);
ctrl.$inject=['$scope', '$location', '$anchorScroll'];
function ctrl($scope, $location, $anchorScroll) {
    $scope.scrollTo = function(team) {
      $location.hash('team');
      $anchorScroll();
    };
    
    $scope.scrollTo = function(contact) {
      $location.hash('contact');
      $anchorScroll();
    };
};

<body ng-controller="ctrl">

<div class="container">
	<div class="fixed-action-btn toolbar">
    <a class="btn-floating btn-large light-blue accent-2 pulse">
      <i class="large material-icons">menu</i>
    </a>
    <ul>
      <li class="waves-effect waves-light"><a ui-sref="home">HOME</a></li>
      <li class="waves-effect waves-light"><a ng-click="scrollTo(home/project)">PROJECTS</a></li>
      <li class="waves-effect waves-light"><a ng-click="scrollTo(team)">TEAM</a></li>
      <li class="waves-effect waves-light"><a ng-click="scrollTo(contact)">CONTACT</a></li>
    </ul>
  </div>
</div>
&#13;
&#13;
&#13;

任何帮助都表示赞赏。
谢谢。

1 个答案:

答案 0 :(得分:0)

你不需要在控制器中添加多个scrollTo功能,你只能使用一个这样的功能

myapp.controller('ctrl', ctrl);
ctrl.$inject = ['$scope', '$location', '$anchorScroll'];

function ctrl($scope, $location, $anchorScroll) {
    $scope.scrollTo = function(state) {
        $location.hash(state);
        $anchorScroll();
    }
};

html是

<body ng-controller="ctrl"> 
  <div class="container">   
    <div class="fixed-action-btn toolbar"> 
      <a class="btn-floating btn-large light-blue accent-2 pulse"> <i class="large material-icons">menu</i> </a> 
      <ul> 
        <li class="waves-effect waves-light"><a ui-sref="home">HOME</a></li> 
        <li class="waves-effect waves-light"><a ng-click="scrollTo('home/project')">PROJECTS</a></li> 
        <li class="waves-effect waves-light"><a ng-click="scrollTo('team')">TEAM</a></li>
        <li class="waves-effect waves-light"><a ng-click="scrollTo('contact')">CONTACT</a></li>
      </ul> 
    </div> 
  </div>
</body>