2017-04-20 89 views
0

我需要你的帮助。 我有两个.factory'sServices.js 第一个.factory与数据库的工作,第二个.factory与电子邮件,文件等。 如何将第一工厂的值传递给第二工厂?如何从第一个工厂中选择数据?如何在离子因子之间传递一个值?

//first factory 

angular.module('starter.services', ['ngCordova', 'ngSanitize', 'ngCsv']) 
.factory('NotesDataService', function ($cordovaSQLite, $ionicPlatform) { 
var db, dbName = "CONTACTS_DB" 

function useWebSql() { 
    db = window.openDatabase(dbName, "1.0", "Contacts database", 200000) 
    console.info('Using webSql') 
} 

function useSqlLite() { 
    db = $cordovaSQLite.openDB({name: dbName, location : 1}) 
    console.info('Using SQLITE') 
} 

function initDatabase(){ 
    $cordovaSQLite.execute(db, 'CREATE TABLE IF NOT EXISTS T_CONTACTS (id integer primary key, nom, prenom, codePostale, ville, email, portable)') 
    .then(function(res){ 

    }, onErrorQuery) 
} 

$ionicPlatform.ready(function() { 
    if(window.cordova){ 
    useSqlLite() 
    } else { 
    useWebSql() 
    } 

    initDatabase() 
}) 

function onErrorQuery(err){ 
    console.error(err) 
} 

return{ 
    getAll: function(callback){ 
    $ionicPlatform.ready(function() { 
     $cordovaSQLite.execute(db, 'SELECT * FROM T_CONTACTS').then(function (results) { 
     var data = [] 

     for (i = 0, max = results.rows.length; i < max; i++) { 
      data.push(results.rows.item(i)) 
     } 

     callback(data) 
     }, onErrorQuery) 
    }) 
    }}) 




//second factory, here I need to get data from first factory 
//to create a text file with the data from the database 
// and attach this file to the e-mail 

.factory('ContactsService', function ($ionicPlatform, $cordovaEmailComposer, $cordovaSQLite, $cordovaFile, NotesDataService) { 



$ionicPlatform.ready(function() { 


initCordovaEmailComposer(); 
    }) 



function initCordovaEmailComposer() { 
    $cordovaEmailComposer.isAvailable().then(function() { 
     //is available 
     alert('avaible'); 
    }, function() { 
     //not available 
     alert('not available'); 
    }) 
    } 

    return { 
    createEmail: function() { 
     var email = { 
     to: '[email protected]', 
     cc: '[email protected]', 
     bcc: '[email protected]', 
     attachments: [ 
      'file://cordova.file.externalDataDirectory/contacts.txt', 
     ], 
     subject: 'Cordova Icons', 
     body: "Hello, mon ami", 
     isHtml: true 
     }; 


    $cordovaEmailComposer.open(email).then(null, function() { 
    }); 
}, 

debugMessage: function (data) { 
    console.log('debug message', data); 
}, 

createFile: function() { 
    var fileContacts = document.addEventListener('deviceready', function() { 
    NotesDataService.getAll(function (data) { 
    console.log(data) 
    return data 
    }) 
    console.log('file contacts in console: ',fileContacts) 
    var fileName = 'contacts.txt' 
    var fileText = fileContacts 
    var filePath = cordova.file.externalDataDirectory 
    //CHECK file 
    $cordovaFile.checkFile(filePath, fileName).then(function (success) { 
     alert("file exist") 
     }, function (error) { 
     alert("file not exist", filePath) 

     //WRITE NEW FILE 
     $cordovaFile.writeFile(cordova.file.externalDataDirectory, fileName, fileText, true).then(function (success) { 
      // success 
     }, function (error) { 
      // error 
     }); 

      }); 
     }) 
    }, 
    } 
    }) 

谢谢大家的建议和支持

回答

0

注入这些工厂到彼此:

.factory( 'NotesDataService',[ '$ cordovaSQLite', '$ ionicPlatform','ContactsService ',函数($ cordovaSQLite,$ ionicPlatform,ContactsService)...

.factory('ContactsService',['$ ionicPlatform',.....''NotesDataService',function($ ionicPlatform,$ cordovaEmailComposer ,$ cordovaSQLite,$ cordovaFile,NotesDataService){

现在您可以在其他工厂使用工厂变量和功能。

+0

谢谢) 我需要更改模板? 我有一个错误 'ionic.bundle.js:26799错误:[$ injector:unpr]未知提供者:NotesDataServiceProvider < - NotesDataService < - ListCtrl' –