2015-08-28 57 views
0

當使用$ anchorScroll滾動到元素時,我有一些問題,你能幫助我嗎?

這是我的源:

ParentController:

var myApp = angular.module('myApp'); 
myApp.controller('ParentCtrl',function($scope){ 

} 

ChildrenController:

var myApp = angular.module('myApp'); 
myApp.controller('ChildrenCtrl',function($scope,$location,$anchorScroll){ 
    $location.hash('tries'); 
    $anchorScroll(); 
    console.log('Hihi');  
} 

的index.html我接收

<div ng-controller="ParentCtrl"> 
    <div ng-view> </div> 
</div> 

結果:

HiHi 

HiHi 

我不知道,爲什麼是孩子控制器調用2次?

請幫幫我!

謝謝!

+0

你有代碼實例化你的ChildrenCtrl,你的HTML顯示你的ParentCtrl被調用,但不是ChildrenCtrl。 – Fred

+0

在ParentCtrl中,我使用$ location.path('/ childrent')轉發ChildrenCtrl的視圖 –

+0

您是否有指令爲ChildrenCtrl? – Fred

回答

0

當您使用$location.hash更新路線時,它看起來像頁面重新加載,因此再次調用控制器。將reloadOnSearch: false添加到您的路由定義中,以防止頁面加載。