angularjs每次显示页面时都会运行函数

angularjs run function every time the page is shown

本文关键字:运行 函数 显示 angularjs      更新时间:2023-09-26

我有这个代码:

.controller('HomeCtrl', function( $scope, $http )
{
    $http.get( onlinePath + "/status" ).then(function(result)
    {
        if( result.data.status == true )
        {
            $scope.currentStatus = result.data.userStatus;
        }
    });
});

加载此页面时(第一次打开时),它将处于联机状态,并在此页面中显示。

问题是,我导航应用程序,当我到达这个页面时(我认为它已经加载),它没有再次运行$http.get。

如何知道页面何时再次打开(未加载)?

看看:http://ionicframework.com/docs/api/directive/ionView/。

具体到"查看生命周期和事件"部分,它说视图是缓存的(你可以配置的缓存),你可以监听一些特定的事件。。阶段。

所以你可以做:

  $scope.$on("$ionicView.beforeEnter", function() {
    console.log("http call here");        
  });

在路由配置中,您可以添加缓存:false参数,例如

.state('app.home', {
    cache: false,
    url: '/home',
    views: {
        'menuContent': {
            templateUrl: 'templates/home.html',
            controller: 'HomeCtrl'
        }
    }
})