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



我在创建新变量(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中调用这些函数的正确方法,将不胜感激。

尝试

从$scope函数中访问$scope时,您缺少几个$

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

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

最新更新