简体   繁体   English

无法在Visual Studio中使用Angular $ q库(Apache cordova)

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

I need to use $q to wait until my async function has completed and then do something. 我需要使用$ q等待异步功能完成,然后再执行某些操作。

However I have tried injecting $q into my angular module as well as my angular functions and I am getting the message $q is undefined. 但是,我尝试将$ q以及我的角度函数注入到我的angular模块中,并且得到消息$ q是未定义的。

Can someone tell me how I can go about being able to use this in my code? 有人可以告诉我如何在代码中使用它吗?

Here is the code for the module and the function I want to use $q in respectively 这是我想分别使用$ q的模块和函数的代码

Module 模组

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

Controller and Function 控制器及功能
In this case I want to wait for the results.forEach to finish then I want to hide my loading screen using $ionicLoading.hide() 在这种情况下,我想等待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() }); 

So i'd probably do something like this 所以我可能会做这样的事情

This is completely untested code so take that for what you will 这是完全未经测试的代码,因此请按照您的意愿进行操作

$q.all is what your going to want to look into $ 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