[android]不定义范围,在 Angularjs 模块中调用的函数

发布时间: 2017/3/4 2:53:30
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想创建一个新的变量 (目标路径),然后调用其他方法来获得所需的视频下载到本地上的正确目录的所有数据时调用一个 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:

你就错过了几个 $ s 当试图访问 $scope 从 $scope 函数中的。

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

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

你可能会混淆依赖注入 (如 $scope、 $http 等在控制器) 与指令的链接功能参数,严格有序 (范围, element ,属性、 控制器)。

赞助商