尝试使用 ngCordova 在设备上本地保存 pdf
Trying to save a pdf locally on device with ngCordova
编辑:没关系。我通过重新添加平台修复了它:
1) 离子平台 rm android
2) 离子平台添加 android
.......
我正在尝试使用 ngcordova 插件 $cordovaFile 保存 pdf。但是我得到一个错误:
未捕获的引用错误:$cordovaFile 未定义,http://192.168.149.151:8100/js/invoice.service.js,行:16
我安装了插件,将脚本包含在 index.html 中,在浏览器和设备上出现了同样的错误。我错过了什么吗?
app.js:
var exampleApp = angular.module('starter', ['ionic','pdf','ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
});
index.html:
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
服务:
angular.module('starter').factory('InvoiceService', ['$q', InvoiceService]);
function InvoiceService($q) {
function createPdf(invoice) {
return $q(function(resolve, reject) {
var dd = createDocumentDefinition(invoice);
var pdf = pdfMake.createPdf(dd);
pdf.getBase64(function (output) {
resolve(base64ToUint8Array(output));
});
pdfMake.createPdf(dd).getBuffer(function (buffer) {
var utf8 = new Uint8Array(buffer); // Convert to UTF-8...
binaryArray = utf8.buffer; // Convert to Binary...
$cordovaFile.writeFile(cordova.file.dataDirectory, "example.pdf", binaryArray, true)
.then(function (success) {
console.log("pdf created");
}, function (error) {
console.log("error");
});
});
});
}
return {
createPdf: createPdf
};
}
插件列表:
ionic plugin list
com.jcjee.plugins.emailcomposer 1.4.6 "Email Composer with Attachments"
cordova-plugin-console 1.0.2 "Console"
cordova-plugin-device 1.1.1 "Device"
cordova-plugin-file 4.1.1 "File"
cordova-plugin-splashscreen 3.2.1 "Splashscreen"
cordova-plugin-statusbar 2.1.2 "StatusBar"
cordova-plugin-whitelist 1.2.1 "Whitelist"
ionic-plugin-keyboard 2.0.1 "Keyboard"
dms-MacBook-Pro:pdf-test dms$ ionic plugin list
com.jcjee.plugins.emailcomposer 1.4.6 "Email Composer with Attachments"
cordova-plugin-console 1.0.2 "Console"
cordova-plugin-device 1.1.1 "Device"
cordova-plugin-file 4.1.1 "File"
cordova-plugin-splashscreen 3.2.1 "Splashscreen"
cordova-plugin-statusbar 2.1.2 "StatusBar"
cordova-plugin-whitelist 1.2.1 "Whitelist"
ionic-plugin-keyboard 2.0.1 "Keyboard"
没关系。我通过重新添加平台修复了它:
1) 离子平台 rm android
2) 离子平台添加 android
您在工厂中使用“$cordovaFile”,但您没有注入您的 factory/service 模块应该是这样的:
angular.module('starter').factory('InvoiceService', ['$q', InvoiceService, '$cordovaFile', function($q,InvoiceService,$cordovaFile) {}]);
或者
angular.module('starter').factory("InvoiceService", function($q,InvoiceService,$cordovaFile) {});
编辑:没关系。我通过重新添加平台修复了它:
1) 离子平台 rm android
2) 离子平台添加 android
.......
我正在尝试使用 ngcordova 插件 $cordovaFile 保存 pdf。但是我得到一个错误:
未捕获的引用错误:$cordovaFile 未定义,http://192.168.149.151:8100/js/invoice.service.js,行:16
我安装了插件,将脚本包含在 index.html 中,在浏览器和设备上出现了同样的错误。我错过了什么吗?
app.js:
var exampleApp = angular.module('starter', ['ionic','pdf','ngCordova'])
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
if(window.cordova && window.cordova.plugins.Keyboard) {
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
// Don't remove this line unless you know what you are doing. It stops the viewport
// from snapping when text inputs are focused. Ionic handles this internally for
// a much nicer keyboard experience.
cordova.plugins.Keyboard.disableScroll(true);
}
if(window.StatusBar) {
StatusBar.styleDefault();
}
});
});
index.html:
<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<script src="cordova.js"></script>
服务:
angular.module('starter').factory('InvoiceService', ['$q', InvoiceService]);
function InvoiceService($q) {
function createPdf(invoice) {
return $q(function(resolve, reject) {
var dd = createDocumentDefinition(invoice);
var pdf = pdfMake.createPdf(dd);
pdf.getBase64(function (output) {
resolve(base64ToUint8Array(output));
});
pdfMake.createPdf(dd).getBuffer(function (buffer) {
var utf8 = new Uint8Array(buffer); // Convert to UTF-8...
binaryArray = utf8.buffer; // Convert to Binary...
$cordovaFile.writeFile(cordova.file.dataDirectory, "example.pdf", binaryArray, true)
.then(function (success) {
console.log("pdf created");
}, function (error) {
console.log("error");
});
});
});
}
return {
createPdf: createPdf
};
}
插件列表:
ionic plugin list
com.jcjee.plugins.emailcomposer 1.4.6 "Email Composer with Attachments"
cordova-plugin-console 1.0.2 "Console"
cordova-plugin-device 1.1.1 "Device"
cordova-plugin-file 4.1.1 "File"
cordova-plugin-splashscreen 3.2.1 "Splashscreen"
cordova-plugin-statusbar 2.1.2 "StatusBar"
cordova-plugin-whitelist 1.2.1 "Whitelist"
ionic-plugin-keyboard 2.0.1 "Keyboard"
dms-MacBook-Pro:pdf-test dms$ ionic plugin list
com.jcjee.plugins.emailcomposer 1.4.6 "Email Composer with Attachments"
cordova-plugin-console 1.0.2 "Console"
cordova-plugin-device 1.1.1 "Device"
cordova-plugin-file 4.1.1 "File"
cordova-plugin-splashscreen 3.2.1 "Splashscreen"
cordova-plugin-statusbar 2.1.2 "StatusBar"
cordova-plugin-whitelist 1.2.1 "Whitelist"
ionic-plugin-keyboard 2.0.1 "Keyboard"
没关系。我通过重新添加平台修复了它:
1) 离子平台 rm android
2) 离子平台添加 android
您在工厂中使用“$cordovaFile”,但您没有注入您的 factory/service 模块应该是这样的:
angular.module('starter').factory('InvoiceService', ['$q', InvoiceService, '$cordovaFile', function($q,InvoiceService,$cordovaFile) {}]);
或者
angular.module('starter').factory("InvoiceService", function($q,InvoiceService,$cordovaFile) {});