【发布时间】:2026-01-27 06:35:02
【问题描述】:
我试图在创建一个新变量 (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.getURL() 和$scope.getFilePath(),控制台会显示什么?
-
是的,我现在意识到了我的错误,我想出于某种原因,我认为它忽略了以前的一些测试中的 $ 字符,但现在使用 $scope 可以正常工作
标签: javascript android angularjs ionic-framework