ng-click是否会触发$ stateChangeStart?

时间:2014-08-05 16:27:31

标签: angularjs angular-ui-router

我正在使用Angular UI Router而且我有一个非常简单:

  <a href='#' ng-click="toggleCollapse()">
    <i class="icon icon-triangle-down" ng-class="{'icon-triangle-down': !isCollapsed,
'icon-triangle-right': isCollapsed}"></i>
  </a>

我的功能如下:

$scope.toggleCollapse = ->
  alert 'here'
  console.log $scope.isCollapsed
  $scope.isCollapsed = !$scope.isCollapsed

但是,出于某种原因,我的

$rootScope.$on '$stateChangeStart', (event, next, nextParams) ->
  console.log event
单击链接时会触发

。有没有办法阻止它触发$stateChangeStart

1 个答案:

答案 0 :(得分:1)

这是因为您有href='#'标记。

 <a ng-click="toggleCollapse()">
    <i class="icon icon-triangle-down" ng-class="{'icon-triangle-down': !isCollapsed,
'icon-triangle-right': isCollapsed}"></i>
  </a>

删除href,它不会触发路由更改