簡體   English   中英

無法在Visual Studio中使用Angular $ q庫(Apache cordova)

[英]Can't use Angular $q library in Visual Studio (Apache cordova)

我需要使用$ q等待異步功能完成,然后再執行某些操作。

但是,我嘗試將$ q以及我的角度函數注入到我的angular模塊中,並且得到消息$ q是未定義的。

有人可以告訴我如何在代碼中使用它嗎?

這是我想分別使用$ q的模塊和函數的代碼

模組

 var droidSync = angular.module('droidSync', ['ionic', 'ngRoute', 'ui.router']); 

控制器及功能
在這種情況下,我想等待results.forEach完成,然后使用$ionicLoading.hide()隱藏我的加載屏幕。

 droidSync.controller('mainController', function ($scope, $ionicLoading) { $scope.syncContacts = function () { //Display a loading screen while sync is in execution $ionicLoading.show({ template: '<p>Syncing Contacts...</p><ion-spinner class="spinner-calm" icon="crescent"/>' }); var table = AzureService.getTable('contact'); table.read().done(function (results) { results.forEach(function (result) { //THIS NEEDS TO BE COMPLETE BEFORE HIDING LOAD SCREEN console.log('result is', result); // If the contact is flagged as deleted check if its on the device and delete it if (result.isdeleted == true) { var options = new ContactFindOptions(); options.filter = result.id; options.multiple = false; var fields = ["*"]; navigator.contacts.find(fields, findSuccess, findError, options); function findSuccess(contact) { if (contact.length > 0) { console.log("inside the delete area:", contact); var contactToDelete = navigator.contacts.create(); //It is safe to use contact[0] as there will only ever be one returned as AzureID is unique contactToDelete.id = contact[0].id; contactToDelete.rawId = contact[0].id; console.log('we want to delete this', contactToDelete); contactToDelete.remove(); console.log('Contact Deleted'); } else { console.log('Contact to delete not present on device. Checking next contact'); } } function findError() { console.log('Contact search failed: Deleted Contact Search'); } } else { //create a contact object to save or update var emails = []; var phoneNumbers = []; var name = new ContactName(); var contactToUpdate = navigator.contacts.create(); contactToUpdate.note = result.id; name.givenName = result.firstname; name.familyName = result.lastname; phoneNumbers[0] = new ContactField('mobile', result.mobilephone, true); phoneNumbers[1] = new ContactField('home', result.homephone, false); emails[0] = new ContactField('work', result.email, true); contactToUpdate.name = name; contactToUpdate.phoneNumbers = phoneNumbers; contactToUpdate.emails = emails; //Search for the contact on the device var options = new ContactFindOptions(); options.filter = result.id; options.multiple = false; var fields = ["*"]; navigator.contacts.find(fields, foundSuccess, foundError, options); function foundSuccess(contact) { if (contact.length > 0) { //The contact has been found on the device. Pass in ids for contact, emails and phone numbers to update. console.log('object to update is object is', contact); console.log('contact array length is ', contact.length); contactToUpdate.id = contact[0].id; contactToUpdate.rawId = contact[0].rawId; contactToUpdate.phoneNumbers[0].id = contact[0].phoneNumbers[0].id; contactToUpdate.phoneNumbers[1].id = contact[0].phoneNumbers[1].id; contactToUpdate.emails[0].id = contact[0].emails[0].id; console.log('about to save this', contactToUpdate); contactToUpdate.save(upSuccess, upError); function upSuccess() { console.log('updated a contact!'); } function upError(ContactError) { console.log('error updating a contact!'); } } else { //The contact does not exist on the device. Just save it. console.log('non existent contact: ', contactToUpdate); contactToUpdate.save(saveSuccess, SaveError); function saveSuccess() { console.log('saved a contact!'); } function SaveError() { console.log('error saving a contact!'); } } } function foundError() { console.log('Contact search failed: Undeleted Contact Search'); } } // end else })) // end forEach }) // table.read() }; // scope.syncContacts() }); 

所以我可能會做這樣的事情

這是完全未經測試的代碼,因此請按照您的意願進行操作

$ q.all是您要研究的內容

droidSync.controller('mainController', ["$scope", "$q", "$ionicLoading", 
    function ($scope, $q, $ionicLoading) {

var loop = function(result){
    var deferred = $q.defer();

    deferred.resolve(// your loop stuff);

    return deferred.promise;
};

var loopingFunction = function(results){
    var promises = [];

    results.forEach(function(result){
        promises.push(loop(result));
    });

    return $q.all(promises);
};


$scope.syncContacts = function () {
    //Display a loading screen while sync is in execution
    $ionicLoading.show({
        template: '<p>Syncing Contacts...</p><ion-spinner class="spinner-calm" icon="crescent"/>'
    });
    var table = AzureService.getTable('contact');
    table.read().done(function (results) {
        loopingFunction(results).then(function(){
            // do something after it finishes
            $ionicLoading.hide()
        });
    });
};
}]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM