ngCordova触点在角度控制器内不可用

ngCordova contacts not available inside angular controller?

本文关键字:控制器 触点 ngCordova      更新时间:2023-09-26

我很困惑为什么不能在控制器内调用$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);
  // });
});

我只想在控制器中获取联系人,而不是每次运行应用程序时。有什么建议吗?

cordova尚未就绪时,控制器window.stupid = $cordovaContacts;中的代码将初始化。

关于Cordova的常见问题,您必须用deviceready事件包装插件调用:

document.addEventListener("deviceready", function () {
  window.stupid = $cordovaContacts;
}, false);
// OR with IONIC
$ionicPlatform.ready(function() {
  window.stupid = $cordovaContacts;
});

因此,您必须编写与您在运行块中已经编写的代码几乎相同的代码!