角度控制器内部没有ngCordova触点?

时间:2016-01-11 06:32:43

标签: javascript angularjs cordova ngcordova

我很困惑,为什么我不能在我的控制器内调用$ionicPlatform。我可以在角度run()方法内部没有问题。

angular.module('starter', ['ionic', 'ngCordova', 'app.controllers', 'app.services'])

.run(function($ionicPlatform, $http, $cordovaContacts, debugService) {
  $ionicPlatform.ready(function() {
    window.stupid = $cordovaContacts;
    // 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);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

当我尝试在控制器内调用时,它是空的。

.controller('ContactsCtrl', function($scope, $ionicPlatform, $cordovaContacts, $http, debugService) {
  window.stupid = $cordovaContacts;
  //debugService.log(ionic);
  // $cordovaContacts.find({filter: '',multiple: true,fields: ['displayName', 'name']}).then(function(allContacts){
  //     alert('cordova contacts found');
  //     debugService.log(allContacts);
  //     // This has issues
  //     $scope.contacts = contactsService.get(allContacts);
  // });

});

我只想抓住控制器内部的联系人,而不是每次运行应用程序。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

window.stupid = $cordovaContacts;尚未准备就绪时,您的控制器cordova中的代码即被启动。

关于Common Issues with Cordova,您必须使用deviceready事件包装插件调用:

document.addEventListener("deviceready", function () {
  window.stupid = $cordovaContacts;
}, false);

// OR with IONIC

$ionicPlatform.ready(function() {
  window.stupid = $cordovaContacts;
});

所以你必须在运行程序段中使用几乎相同的代码!