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

标签 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 中调用这些函数的正确方法,我将不胜感激。

最佳答案

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

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

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

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

关于javascript - 范围未定义,调用 Angularjs 模块中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41867149/

相关文章:

java - Android:如何在不充电时将电池统计信息添加到库存锁定屏幕?

angularjs - 如何使用 https ://angular. io/docs/ts/latest/guide/webpack.html 中的 vendor.ts

javascript - 使用 AngularJS 和 PHP 将数据保存到数据库

javascript - 子菜单,使 div 出现

java - 使用 ScaleGestureDetector 时放大太多后无法缩小

Android 在添加 spinnerSelector 时出错,找不到资源标识符

javascript - 双向数据绑定(bind)(Angular)与单向数据流(React/Flux)

javascript - 如何将输入值从 HTML 传递到 JavaScript 以用于切换包含函数的情况

javascript - 显示 Cordova 中下载文件的百分比

javascript 跳线 safari