0

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.

2
  • 1
    Why you use "scope" instead of '$scope"? What console will say if you use $scope.getURL() and $scope.getFilePath()? Commented Jan 26, 2017 at 5:03
  • Yeah I realise my mistake now, I think for some reason I thought it was ignoring the $ character on some previous testing but it's working fine now with $scope Commented Jan 26, 2017 at 6:23

1 Answer 1

3

You're missing several $s when attempting to access $scope from within $scope functions.

$scope.something = function () {
  //This is the scope
  console.log($scope);

  //This is undefined
  console.log(scope);
}

You may be confusing Dependency Injection (like $scope, $http, etc in controllers) with directive's link function parameters, which are strictly ordered (scope, element, attributes, controller).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your example explained my errors perfectly! My app is now functioning as expected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.