I have problem when make an ionic app for IOS about cannot generate PDF file after get data from $localStorage using ngStorage plugin and PDFmake plugin . Its my controllers.js script below:
angular.module('starter.controllers', [])
.controller('AppCtrl', function($scope, $ionicModal, $timeout) {
// Form data for the login modal
$scope.loginData = {};
// Create the login modal that we will use later
$ionicModal.fromTemplateUrl('templates/login.html', {
scope: $scope
}).then(function(modal) {
$scope.modal = modal;
});
// Triggered in the login modal to close it
$scope.closeLogin = function() {
$scope.modal.hide();
};
// Open the login modal
$scope.login = function() {
$scope.modal.show();
};
// Perform the login action when the user submits the login form
$scope.doLogin = function() {
console.log('Doing login', $scope.loginData);
// Simulate a login delay. Remove this and replace with your login
// code if using a login system
$timeout(function() {
$scope.closeLogin();
}, 1000);
};
})
.controller('PlaylistsCtrl', ['$scope', '$location', '$localStorage', function($scope, $location, $localStorage) {
$scope.model = {
phone: '',
total: '',
type: '',
discount: '',
};
$scope.producttype = [{
product: "A",
value: 7900000,
discount1: 5612,
discount2: 79000
}, {
product: "B",
value: 10200000,
discount1: 5612,
discount2: 79000
}, {
product: "C",
value: 11000000,
discount1: 5612,
discount2: 79000
}, {
product: "D",
value: 7300000,
discount1: 5612,
discount2: 79000
}, {
product: "E",
value: 10000000,
discount1: 5612,
potongan2: 79000
}, {
product: "F",
value: 11200000,
discount1: 5612,
discount2: 79000
}, ];
$scope.create = function(model) {
$scope.model.total = $scope.model.phone + 3;
$localStorage.model = $scope.model;
console.log($localStorage.model);
$location.path('/app/more');
}
}])
.controller('MoreCtrl', ['$scope', '$ionicHistory', '$localStorage', function($scope, $ionicHistory, $localStorage) {
$scope.model = $localStorage.model;
if ($scope.model.type == "Good") {
$scope.model.selection = $scope.model.type.discount1;
console.log($scope.model.discount);
} else {
$scope.model.selection = $scope.model.type.discount2;
console.log($scope.model.discount);
}
var binaryArray = null;
var currentfileEntry = null;
$scope.docDefinition = {
content: [
{
text: 'Total Number',
style: 'header'
},
{
text: $scope.model.phone,
style: 'header'
},
{
text: $scope.model.selection,
style: 'header'
},
],
styles: {
header: {
fontSize: 18,
bold: true,
alignment: 'center'
},
subheader: {
fontSize: 15,
bold: true
},
table: {
margin: [0, 5, 0, 15]
},
quote: {
italics: true
},
small: {
fontSize: 8
}
} };
function fail(error) {
console.log(error.code);
};
function gotFS(fileSystem) {
var fileName = "your-file.pdf";
fileSystem.root.getFile(fileName, {
create: true,
exclusive: false
}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
currentfileEntry = fileEntry;
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwrite = function (evt) {
$scope.downloading = false;
alert("The file was written successfully");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFSforRead, fail);
}
writer.write(binaryArray);
}
$scope.createpdf = function(model) {
$localStorage.model = $scope.model;
console.log ($scope.docDefinition);
if (!window.cordova) {
pdfMake.createPdf($scope.docDefinition).open();
} else {
pdfMake.createPdf($scope.docDefinition).getBuffer(function (buffer) {
var utf8 = new Uint8Array(buffer); // Convert to UTF-8...
binaryArray = utf8.buffer; // Convert to Binary...
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
});
}
}
}]);
I've got an error message on console log like "undefined" for $scope.model.selection after trying generate to a PDF file but can show on list. For more scripts for this problem you can download my example code on my repo.
How I can fix it?