AngularJS - Ionic:返回时在控制器中执行代码
AngularJS - Ionic : execute code in controller when coming back to it
我是 AngularJS 社区的新成员,我正在使用此框架开发我的第一个应用程序。
我用这段代码创建了一个新控制器:
.controller('AccountCtrl', function($scope, $location) {
alert('test');
})
还有我的路线:
.state('app.account', {
url: "/account",
views: {
'menuContent': {
templateUrl: "templates/account.html",
controller: 'AccountCtrl'
}
}
})
我第一次访问控制器时显示警报弹出窗口。但是,如果我更改 URL 并返回到 AccountCtrl(使用经典的 html a),则不会再次显示警报弹出窗口。
有人可以向我解释为什么吗?
感谢您的帮助!
每次在 ui 路由器中重新加载控制器,使用 .state
上的 reload: true
选项
$stateProvider
.state('app.account', {
url: "/account",
reload: true //forcefully reload route and load controller again
})
在 Ionic Framework 中,默认情况下会缓存视图和控制器。您可以向视图范围添加一个侦听器,以便在视图再次重新激活时接收通知。有关详细信息,请参阅: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');
});
})`
我是 AngularJS 社区的新成员,我正在使用此框架开发我的第一个应用程序。
我用这段代码创建了一个新控制器:
.controller('AccountCtrl', function($scope, $location) {
alert('test');
})
还有我的路线:
.state('app.account', {
url: "/account",
views: {
'menuContent': {
templateUrl: "templates/account.html",
controller: 'AccountCtrl'
}
}
})
我第一次访问控制器时显示警报弹出窗口。但是,如果我更改 URL 并返回到 AccountCtrl(使用经典的 html a),则不会再次显示警报弹出窗口。
有人可以向我解释为什么吗?
感谢您的帮助!
每次在 ui 路由器中重新加载控制器,使用 .state
上的reload: true
选项
$stateProvider
.state('app.account', {
url: "/account",
reload: true //forcefully reload route and load controller again
})
在 Ionic Framework 中,默认情况下会缓存视图和控制器。您可以向视图范围添加一个侦听器,以便在视图再次重新激活时接收通知。有关详细信息,请参阅: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');
});
})`