I am trying to call a method when creating a new variable (targetPath) which then calls other methods to get all the data needed to download a video to the correct directory on local android storage. However I am having trouble calling the methods as my method called either gets undefined or if I used $scope.callMethod, scope gets undefined.
var targetPath = scope.getFilePath();
//Gets the URL of where to download from
$scope.getURL = function () {
//Whatever URL we want
var url = "http://static.videogular.com/assets/videos/videogular.mp4";
return url;
}
//Gets the filename of any URL we download from
$scope.getFileName = function () {
//Splits the URL
var filename = scope.getURL().split("/").pop();
return filename;
}
//This function is used to get the directory path so we can use it for other functions
$scope.getFilePath = function () {
//Use this code for internal file download
var targetPath = cordova.file.externalDataDirectory + scope.getFileName();
return targetPath;
}
FULL CODE HERE
angular.module('starter.controllers', [])
.controller('AppCtrl', function ($scope, $ionicModal, $timeout, $cordovaFileTransfer, $sce) {
//Gets the URL of where to download from
$scope.getURL = function () {
//Whatever URL we want
var url = "http://static.videogular.com/assets/videos/videogular.mp4";
return url;
}
//Gets the filename of any URL we download from
$scope.getFileName = function () {
//Splits the URL
var filename = scope.getURL().split("/").pop();
return filename;
}
//This function is used to get the directory path so we can use it for other functions
$scope.getFilePath = function () {
//Use this code for internal file download
var targetPath = cordova.file.externalDataDirectory + scope.getFileName();
return targetPath;
}
//download file function
$scope.downloadFile = function () {
//Keeps track of progress bar
var statusDom = document.querySelector('#status');
var myProgress = document.querySelector("#myProgress");
//URL where the video is downloaded from
//var url = "http://static.videogular.com/assets/videos/videogular.mp4";
//Splits the URL
// var filename = url.split("/").pop();
//alert(filename);
//Interal storage
//Use this code for internal file download
var targetPath = scope.getFilePath();
var trustHosts = true
var options = {};
//Makes sure that the URL is trusted to get around permission issues at download
console.log($sce.trustAsResourceUrl(scope.getURL()));
$cordovaFileTransfer.download(scope.getURL(), scope.getFilePath(), options, trustHosts)
.then(function (result) {
// Success!
alert(JSON.stringify(result));
}, function (error) {
// Error
alert(JSON.stringify(error));
}, function (progress) {
//Shows how much the file has loaded
if (progress.lengthComputable) {
var perc = Math.floor(progress.loaded / progress.total * 100);
statusDom.innerHTML = perc + "% loaded...";
myProgress.value = perc;
} else {
if (statusDom.innerHTML == "") {
statusDom.innerHTML = "Loading";
} else {
statusDom.innerHTML += ".";
}
}
})
}
})
If anyone could please show me the correct way to call these functions in angularjs that would be much appreciated.