9

我正在尝试将 MovilizerJS 与 Ionic 框架一起使用来创建 HTML5 屏幕。我尝试从 Ionic 生成的 App.js 文件中引用 te MovilizerJS。我在插件文件夹中添加了 MovilizerJS 文件,并添加了包含的 Cordova.js 文件。

var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript = document.createElement("script");
oScript.type = "text/javascript";
oScript.src = "plugins/Movilizer.js";
oHead.appendChild(oScript);

似乎当我在浏览器中加载 HTML5 页面(或 movelet 中的 html5 视图)时,MovilizerJS 没有被加载。浏览器上出现以下错误:

模块“movilizer”不可用!

也许我需要将此作为模块添加到 Angular 框架中,但是当我尝试将其添加到模块时,它仍然会给我错误。我的 HTML 文件包含 movilizer 的脚本标签:

<script src="plugins/Movilizer.js"></script>

我的 App.js 代码目前如下所示:

angular.module('starter', ['ionic'])
.run(function($ionicPlatform) { 
      $ionicPlatform.ready(function() {
        // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
        // for form inputs)
        if(window.cordova && window.cordova.plugins.Keyboard) {
          cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
        }
        if(window.StatusBar) {
          StatusBar.styleDefault();
        }

      });
    }).factory('MovilizerExtender', function ($rootScope) {
      return {
        startUpMovilizer: function(){
          movilizer.readGlobalVariable("testTable",this.successCallback,this.errorCallback);
        },
        successCallback: function(result){
          $rootScope.routestops = [
            { ontvNaam: 'nice' },
            { ontvNaam: 'it' },
            { ontvNaam: 'is' },
            { ontvNaam: 'working' }
          ];
       },
       errorCallback: function(){
          console.log('failed');
       }
      }
    }).controller("RoutestopCtrl", function($scope, $rootScope, MovilizerExtender) {
      MovilizerExtender.startUpMovilizer();
      $scope.routestops = $rootScope.routestops;

      $rootScope.$watch('routestops', function(){
          $scope.routestops = $rootScope.routestops;
      });
    });

当我直接调用 succesCallback 方法并注释以下行时:movilizer.readGlobalVariable(...),它不再尝试访问 movilizerJS 并且页面正常工作。另请注意,Movilizer.js 文件包含 app.js 代码中描述的 readGlobalVariable 方法。任何帮助或想法将不胜感激。

4

1 回答 1

0

尝试引导您的应用程序而不是使用ng-app

window.onpageshow = ready;
function ready()
{
    var deviceready = new Event("deviceready");
    document.dispatchEvent(deviceready);   
    angular.bootstrap(document, ['starter'], {strictDi: true});  
}

此外,使用承诺等待movilizer

 .factory('MovilizerExtender', function ($q, $rootScope) {
    return {
      startUpMovilizer: function(){
        var self = this;
        var defer = $q.defer();

        rootScope.$watch(function(value) {
          return movilizer && movilizer.readGlobalVariable;
        }, function(ready) {
          if(ready) {
            movilizer.readGlobalVariable("testTable",self.successCallback,self.errorCallback);
          }
          defer.resolve(ready);
        });
        return defer.promise;
      },
      successCallback: function(result){
        $rootScope.routestops = [
          { ontvNaam: 'nice' },
          { ontvNaam: 'it' },
          { ontvNaam: 'is' },
          { ontvNaam: 'working' }
        ];
      },
      errorCallback: function(){
        console.log('failed');
      }
    }
  }).controller("RoutestopCtrl", function($scope, $rootScope, MovilizerExtender) {
    MovilizerExtender.startUpMovilizer().then(function(){
      $scope.routestops = $rootScope.routestops;
    });
    $rootScope.$watch('routestops', function(){
      $scope.routestops = $rootScope.routestops;
    });
  });
于 2016-02-11T17:55:30.113 回答