2015-04-01 57 views
0

我是AngularJS社區中的新成員,我正在使用此框架開發我的第一個應用程序。AngularJS - Ionic:在回到控制器時執行代碼

我創建了這個代碼,新的控制器:

.controller('AccountCtrl', function($scope, $location) { 
alert('test'); 
}) 

我的路線:

.state('app.account', { 
    url: "/account", 
    views: { 
     'menuContent': { 
     templateUrl: "templates/account.html", 
     controller: 'AccountCtrl' 
     } 
    } 
    }) 

警報彈出顯示我第一次訪問控制器。但是,如果我更改URL並回到AccountCtrl(使用經典html a),則不會再顯示警報彈出窗口。

難道有人向我解釋爲什麼?

謝謝你的幫助!

+0

這是奇怪的,正常的控制器將被實例化,每次URL變化緩存。你在用Ionic框架開發嗎?在Ionic中,觀點和控制器都是兌現的。你可以添加一個Lisisterner到範圍'$ scope。$ on('$ ionicView.beforeEnter',myUpdateFunction'。 – 2015-04-01 09:02:40

+0

Hi Stefan,是的,我正在使用Ionic Framework.I。我必須添加這個監聽器嗎?在我的控制器中? – user1767541 2015-04-01 09:12:14

+0

我添加了一個例子作爲你的問題的答案。希望它有幫助 – 2015-04-01 10:30:36

回答

1

離子框架視圖和控制器將被默認緩存使用reload: true選項。您可以添加偵聽器到視圖範圍,以便在視圖再次重新激活時接收通知。欲瞭解更多信息,請參閱:http://ionicframework.com/docs/api/directive/ionView/http://ionicframework.com/docs/api/directive/ionNavView/

您也可以禁用視圖<ion-view cache-view="false">

.controller('AccountCtrl', function($scope, $location) { 
    $scope.$on('$ionicView.beforeEnter', function() { 
      // update campaigns everytime the view becomes active 
      // (on first time added to DOM and after the view becomes active after cached 
     alert('test'); 
    }); 
})` 
+0

Thanx它適合我!感謝您的幫助 – user1767541 2015-04-01 12:43:49

1

每次重裝控制器在UI路由器,在.STATE

$stateProvider 
.state('app.account', { 
     url: "/account", 
     reload: true //forcefully reload route and load controller again 
}) 
+0

嗨AB,你的答案不適合我:/是否因爲我使用Ionic Framework? – user1767541 2015-04-01 09:24:10

+0

重新加載是否應該在參數更改後重建狀態http://angular-ui.github.io/ui-router/site/#/api/ui.router.state.$state – 2015-04-01 10:32:59

相關問題