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

AngularJS execute controller method on page redirect

本文关键字:执行 控制器 方法 重定向 AngularJS      更新时间:2023-09-26

我用Cordova/Phonegap、Ionic和AngularJS构建了一个应用程序。我想从外部的控制器执行一个内部方法。当我重定向到另一个html(secondPage.html)时,应该执行这个内部方法(secondMethod())。我的方法运行一个SQLite查询,以获取一些数据,并将其显示在新页面上。我试着用一个onload函数,但我的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>

提前谢谢。

通过使用$ionicView.enter监听器来执行您的函数?您也可以尝试使用ui-router 中的resolve功能