每当打开/显示视图时运行控制器功能 [英] Run a controller function whenever a view is opened/shown

查看:25
本文介绍了每当打开/显示视图时运行控制器功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个带有 angular+ionic 的应用程序,它在底部使用经典的三按钮菜单,其中包含三个离子选项卡.当用户单击选项卡时,该模板将通过 ui-router 打开.

我有这样的状态:

$stateProvider.state('其他', {网址:/其他",摘要:真实,templateUrl:模板/其他/other.html";})

在模板中,我执行以下操作:

</ion-nav-view>

我知道我可以在我的控制器中编写 doSomething() 函数,然后在那里手动调用它.不过,这给了我同样的问题.每当有人打开该视图时,我似乎无法弄清楚如何多次调用 doSomething() 函数.

现在,doSomething() 函数被调用得很好,但只有在用户第一次打开视图/选项卡时.每当用户打开该视图或选项卡时,我都想调用一个函数(以更新地理位置).

实现这一目标的正确方法是什么?

解决方案

如果您已将某个控制器分配给您的视图,那么每次您的视图加载时都会调用您的控制器.在这种情况下,您可以在调用控制器后立即执行一些代码,例如这样:

<ion-nav-view ng-controller="indexController" name="other" ng-init="doSomething()"></ion-nav-view>

在你的控制器中:

app.controller('indexController', function($scope) {/*直接在这里写一些代码,没有任何功能,它会在每次你的视图加载时执行.像这样的东西:*/$scope.xyz = 1;});

您可能会尝试跟踪状态更改,然后在路由更改和访问某个路由时执行一些代码,例如:

$rootScope.$on('$stateChangeSuccess',函数(事件,toState,toParams,fromState,fromParams){ ... })

您可以在此处找到更多详细信息:状态变化事件.>

I'm building an app with angular+ionic that uses a classic three-button menu at the bottom with three ion-tabs in it. When a user clicks a tab, that template opens through ui-router.

I have states like this:

$stateProvider
  .state('other', {
    url: "/other",
    abstract: true,
    templateUrl: "templates/other/other.html"
})

In the template I do something like:

<ion-nav-view name="other" ng-init="doSomething()"></ion-nav-view>

I'm aware that I can write the doSomething() function in my controller and just call it manually there. That gives me the same problem though. I can't seem to figure out how to call the doSomething() function more than once, whenever somebody opens that view.

Right now, the doSomething() function gets called just fine, but only the first time that view/tab gets opened by the user. I'd like to call a function (to update geolocation) whenever a user opens that view or tab.

What would be a correct way to implement that?

解决方案

If you have assigned a certain controller to your view, then your controller will be invoked every time your view loads. In that case, you can execute some code in your controller as soon as it is invoked, for example this way:

<ion-nav-view ng-controller="indexController" name="other" ng-init="doSomething()"></ion-nav-view>

And in your controller:

app.controller('indexController', function($scope) {
    /*
        Write some code directly over here without any function,
        and it will be executed every time your view loads.
        Something like this:
    */
    $scope.xyz = 1;
});

Edit: You might try to track state changes and then execute some code when the route is changed and a certain route is visited, for example:

$rootScope.$on('$stateChangeSuccess', 
function(event, toState, toParams, fromState, fromParams){ ... })

You can find more details here: State Change Events.

这篇关于每当打开/显示视图时运行控制器功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆