2017-04-20 17 views
0

私はあなたの助けが必要です。 私は2つ持っています.factory'sServices.js 最初の.factoryは、データベース、第2の.factory電子メール、ファイルなどと動作します。 最初の工場から2番目の工場に値を渡す方法は?最初の工場からデータを選択するには?Ionicのファクトリ間で値を渡す方法は?

.factory( 'NotesDataService'、[ '$ cordovaSQLite'、 '$ ionicPlatform'、 'C​​ontactsService:

//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

がお互いにこれらの工場を注入し、あなたのアドバイスや支援ありがとうございました($ codeovaSQLite、$ ionicPlatform、ContactsService)。

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

他の工場では、工場変数と関数のどちらも使用できます。

+0

ありがとう) テンプレートを変更する必要がありますか? 私はエラーがあります 'ionic.bundle.js:26799エラー:[$ injector:unpr]不明なプロバイダ:NotesDataServiceProvider < - NotesDataService < - ListCtrl' –