Problem cannot read from $scope
Closed this issue · 6 comments
Hello,
I have problem with this plugin especially cannot generate data after read from $scope like my example below:
var docDefinition = {
content: [
{ text: $scope.model.name }
],
};
A $scope.model.name variable get from previous page
Not sure how you are using this code snippet in the overall app. And, I am far from an angular expert, but I would guess is the problem results from using the $scope in the docDef object. The docDef is eventually passed to the actual pdfmake engine for rendering. And, this code is not in the actual angularjs scope/domain. So, the $scope reference (and the attached model object) is probably invalid.
NOTE - you also appear to have a syntax error w/ a trailing comma (,) on the content array that should be removed since you only have a single text element in the content array.
If you look at my code (www/js/reportBuilderSvc.js), I return the docDef (line-32) from the ReportBuilderSvc in the _generateReport method (line-21). You could modify this by accepting a name parameter in the _generateReport method. Then, in your controller, inject the ReportBuilderSvc and call the _generateReport and pass the $scope.model.name as an argument from the controller to the service. Or, as a quick and dirty approach, you could just write the method _generateReport into your controller.
Something like:
//build a service with method to generate report JSON objects
function _generateReport(reportName) {
// use the reportName parameter to build a docDefinition
var docDefinition = {
content: [
{ text: reportName}
]
};
//return the docDefinition
return docDefinition;
}
//controller method to be used on a button in the UI
function _generateReportClick($scope, ReportBuilderSvc) {
//if you follow my design and use a report building service
var docDef = ReportBuilderSvc.generateReport($scope.model.name);
//or, if you just build the report in a method local to the controller
var docDef = _generateReport($scope.model.name);
//go on to pass docDef to pdfmake to render a report
...
}
I downloaded your repo and ran in the browser. The line that I think is creating an issue for you is line #93:
$scope.model.selection = $scope.model.type.discount1;
The type property of model is undefined and therefore when you assign to $scope.model.selection, the scope variable is undefined and causes problems in the docDef. I notice in the debugger that $scope.model.discount is defined and has discount1 and discount2 as properties. Is this what you meant to reference? Perhaps this would work as a replacement for lines #93 and #97?
$scope.model.selection = $scope.model.discount.discount1;
$scope.model.selection = $scope.model.discount.discount2;
Thanks for your help. I will test it later.
I never heard back so I assume the issue was resolved? I will close this issue in a day if I don't hear back on this.
Its already resolved.Thanks!