如何在AngularJS中使用该函数之外的变量
How can I use variable outside that function in AngularJS?
我在控制器中使用这个范围:
$scope.uploadPic = function (file) {
file.upload = Upload.upload({
url: 'http://sites.net/upload',
data: {file: file, blobType: 'apps'},
});
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
var downloadLink = response.data.data.uploadInfo.publicDownloadUrl;
console.log(downloadLink);
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
}, function (evt, response) {
// Math.min is to fix IE which reports 200% sometimes
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
});
}
是的,它有效,我需要这个:
var downloadLink = response.data.data.uploadInfo.publicDownloadUrl;
console.log(downloadLink);
好的,但现在,我必须在另一个函数中使用这个var
。我该怎么做?谢谢
您可以在函数外声明变量:
var downloadLink;
$scope.uploadPic = function (file) {
// ..
file.upload.then(function (response) {
$timeout(function () {
file.result = response.data;
downloadLink = response.data.data.uploadInfo.publicDownloadUrl;
// or, if you want to use it in your view:
// $scope.downloadLink = downloadLink;
console.log(downloadLink);
});
}, function (response) {
// ..
}
function doSomethingWithLink() {
var link = downloadLink;
// do something with link
}
一个解决方案是document.variable
内部函数示例:
document.downloadLink = response.data.data.uploadInfo.publicDownloadUrl;
外部功能示例:
document.downloadLink= response.data.data.uploadInfo.publicDownloadUrl;
您可以像一样在$scope
中绑定它
$scope.downloadLink = response.data.data.uploadInfo.publicDownloadUrl;
因此它可以在文件中全局使用,也可以在模板上全局使用。
或
您可以在函数外声明它以全局使用
相关文章:
- 访问嵌套函数结构中的JavaScript父函数变量
- Javascript函数变量传递语法问题
- JavaScript 函数变量范围问题
- 在 promise 中获取构造函数变量
- 将函数结果传递给Javascript中的另一个函数变量
- 用对象设置函数变量's值
- Javascript函数变量应生成错误,而不是未定义的错误
- 我可以访问用户脚本中的匿名函数变量吗?
- 在实例化期间使用构造函数变量
- 为什么在 JavaScript 中不能用函数变量覆盖字符串变量
- JavaScript 允许调用函数访问调用函数变量
- 如何在嵌套原型中访问 JavaScript 构造函数变量
- 对函数变量的 JavaScript 控制台日志输出感到困惑
- 如何将函数变量传递给主干模型上的 jQuery 事件
- 如何访问构造函数变量
- 实例函数变量不会更改
- 为什么函数变量在变量之后是未定义的
- 用内部函数变量更改外部函数变量?Javascript
- Javascript - 使用闭包递增静态函数变量模拟
- 异步 JavaScript 静态函数变量