未定义范围,在Angularjs模块中调用函数

时间:2017-01-26 04:57:52

标签: javascript android angularjs ionic-framework

我正在尝试在创建新变量(targetPath)时调用一个方法,然后调用其他方法来获取将视频下载到本地Android存储上的正确目录所需的所有数据。但是我调用方法时遇到问题,因为我的方法被称为未定义或者如果我使用$ scope.callMethod,则范围变得未定义。

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;
}

完整的代码在这里

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 += ".";
              }
          }
      })

}
})

如果有人可以请告诉我在angularjs中调用这些函数的正确方法,我们将非常感激。

1 个答案:

答案 0 :(得分:3)

尝试从$ scope函数中访问$ scope时,您错过了几个$

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

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

您可能会混淆依赖注入(如控制器中的$ scope,$ http等)与指令的链接函数参数,这些参数是严格排序的(范围,元素,属性,控制器)。