AngularJS在页面重定向上执行控制器方法

时间:2015-03-19 15:09:58

标签: javascript angularjs controller onload ionic

我使用Cordova / Phonegap,Ionic和AngularJS构建应用程序。我想从外部执行一个来自控制器的内部方法。当我重定向到另一个html(secondPage.html)时,应该执行这个内部方法(secondMethod())。我的方法运行SQLite查询,以获取一些数据,并应在新页面上显示它。我尝试使用onload-function,但我的console.log说,它无法找到我的功能。我怎么能这样做?

app.js

    ionicApp.controller("FirstController", function($scope) {

       $scope.firstMethod = function() {
          window.location.replace("secondPage.html");
       }

    }

    ionicApp.controller("SecondController", function($scope) {

       $scope.secondMethod = function() {
          // do some code
       }

       $scope.otherMethod = function() {
          // some other code
       }

    }

secondPage.html

    // html and head
    <body>
       <ion-content ng-controller="SecondController">
          <label id="text_label">Text</label>
          <button class="button" ng-click="otherFunction()">Button</button>
       </ion-content> 
       <script>
          window.onload=secondMethod();
       </script>
     </body>

提前谢谢。

1 个答案:

答案 0 :(得分:0)

通过使用$ionicView.enter监听器来执行你的功能?您还可以尝试使用ui-router

中的resolve功能