角/离子 - 控制器只运行一次 [英] Angular / Ionic - controller only runs once

查看:192
本文介绍了角/离子 - 控制器只运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始接触角通过离子框架,但我不能明白为什么控制器只运行一次,也就是我改变状态,控制器的运行,切换到另一种状态,然后再次与控制器不运行第二次。这是我的状态:

I'm starting out with Angular through the Ionic Framework but I am failing to understand why the controller only runs once i.e. I change state, the controller runs, change to another state and then back again and the controller does not run a second time. This is my state:

$stateProvider.state( 'container.previous', {
    url: 'previous',
    views: {
        main : {
            templateUrl : 'views/previous.html',
            controller : function( $scope, $cordovaSQLite ){
                $scope.firms = [];
                $cordovaSQLite.execute(window.db, "SELECT * FROM recent GROUP BY phone ORDER by id DESC").then(function(res) {
                    for (i = 0; i < res.rows.length; i++) {
                        $scope.firms.push(res.rows.item(i));
                    }
                }, function (err) {
                    console.error(err);
                });
            }
        }
    },
    onStateChangeStart : function(){
        backButton = true;

    }
});

在另一种状态,如果你点击相关的坚定的按钮,这样可以节省的企业的数据到本地存储。上述状态表示其中有previously点击的公司。但我不能想出如何更新 $ scope.firms 正确的控制器永远不会再次运行。

In another state, if you click on a button related to a "firm", it saves the "firms" data to local storage. The above state shows the firms in which you have previously clicked on. But I cannot figure out how to update the $scope.firms correctly as the controller never runs again.

谁能帮我?

推荐答案

您可以把code要在$ ionicView.enter运行:

You can put the code you want to run in $ionicView.enter:

controller : function( $scope, $cordovaSQLite ){
    $scope.$on('$ionicView.enter', function() {
        // code to run each time view is entered
    });

    ...
});

请参阅查看生命周期和事件:<一href=\"http://ionicframework.com/docs/api/directive/ionView/\">http://ionicframework.com/docs/api/directive/ionView/

See "View LifeCycle and Events": http://ionicframework.com/docs/api/directive/ionView/

这篇关于角/离子 - 控制器只运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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