gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:09:44 26 4
gpt4 key购买 nike

我试图在创建新变量(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com