我有一個離子:頁方法調用輸入
< ion-side-menu >鏈接到我的網頁定義如下:
app.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
$stateProvider.state('content', {
url: "/content",
abstract: true,
templateUrl: "templates/sidemenu.html",
controller: 'SideController'
});
$stateProvider.state('content.home', {
url: "/home",
views: {
'menuContent': {
templateUrl: "templates/home.html",
controller: "HomeController"
}
}
});
$stateProvider.state('content.nearby', {
url: "/nearby",
views: {
'menuContent': {
templateUrl: "templates/nearby.html",
controller: "NearbyController"
}
}
});
$stateProvider.state('content.map', {
url: "/map",
views: {
'menuContent': {
templateUrl: "templates/map.html",
controller: "MapController"
}
}
});
$stateProvider.state('content.radar', {
url: "/radar",
views: {
'menuContent': {
templateUrl: "templates/radar.html",
controller: "RadarController"
}
}
});
$stateProvider.state('content.location-details', {
url: "/location-details/:index",
views: {
'menuContent': {
templateUrl: "templates/location-details.html",
controller: "DetailsController"
}
},
resolve: {
currentLocation: function($stateParams, shareService, NearbyFactory)
{
return NearbyFactory.getLocations()[$stateParams.index];
}
}
});
$urlRouterProvider.otherwise("/content/home");
});
我想在我的控制器執行的方法,當用戶瀏覽這個頁面,頁面時(用於加載AJAX數據或開始聆聽一些Cordova傳感器)。就像這樣:
app.controller("HomeController", function()
{
$scope.onEnter = function(previous_page)
{
...
};
$scope.onExit = function(next_page)
{
...
};
});
我已經嘗試過的OnEnter和的OnExit的$ stateProvider狀態內,但據我所知,我沒有我的$範圍存在。
什麼是最簡單/最好/最好的方式來獲得此功能?如果我能確定上一頁/下一頁,並且用戶導航回退/前進,那將是非常好的。我試過這個:
$scope.$on('$routeChangeStart', function(event, next, current)
{
console.log(next);
});
但是這並沒有每次都工作,它並沒有在加載頁面時觸發。這對我來說似乎也有點骯髒,因爲我必須在每一個控制器中實現這一點。
謝謝!