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

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

问题描述

我建设与使用经典的三键式菜单底部用了三份离子标签角+离子的应用程序。当用户点击一个选项卡,该模板通过UI路由器打开。

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.

我有这样的状态:

$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>

我知道,我可以写在我的控制器中的doSomething()函数,并调用它手动那里。 ,虽然给了我同样的问题。我似乎无法弄清楚如何调用DoSomething的()函数不止一次,每当有人打开这个观点了。

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.

现在,DoSomething的()函数被调用就好了,但只有第一次查看/标签被用户打开。我想调用一个函数(更新地理定位),只要用户打开该视图或标签。

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?

感谢您的帮助了!

推荐答案

如果您分配了一定的控制你的看法,那么你的控制器将被调用每次你的看法加载时间。在这种情况下,你可以在你的控制器,只要它被调用执行某些code,例如是这样的:

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>

和在你的控制器:

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;
});

编辑::您可以尝试跟踪状态更改,然后执行一些code的路线发生改变,并参观了某条路线,例如:

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天全站免登陆