Angular service/factory - class 属性 未在 cordova 文件传输中更新
Angular service/factory - class property not updating inside the cordova file transfer
我有 Angular 在移动应用程序中执行 Cordova 文件传输上传的服务 - 它将文件上传到媒体服务器并 returns 一个对象。我正在尝试将此对象传递给 media_response
属性 但我运气不好 - 我做错了什么?
请注意 - 我已经尝试使用 service
和 factory
,但似乎仍然没有任何乐趣 - $cordovaFileTransfer upload
块中的任何内容都不会传递给 class 一些奇怪的原因的属性。
我希望看到 media_response
具有与数据变量相同的输出,数据变量是从媒体服务器返回的对象,但它显示 no response
知道为什么我没有收到预期的回复吗?
// 预期响应
UploadService.media_response in the controller should be an object to match the console.log(data)
//实际响应
UploadService.media_response is the string 'no response'
//上传服务
abcdServices.service('UploadService', function($http, $localStorage, $location, $q, $rootScope, $cordovaFileTransfer) {
var UploadService = function() {
this.upload_in_progress = false;
this.progress = 0;
this.media_response = '';
};
UploadService.prototype.cordovaFileUpload = function (filename, url, targetPath) {
this.upload_in_progress = true;
this.media_response = 'no response';
var options = {
id: new Date() . getTime() + filename,
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params : {'fileName': filename}
};
$cordovaFileTransfer.upload(url, targetPath, options).then(
function(result) {
// Success!
var data = JSON.parse(result.response);
console.log('file uploaded - response:', data); // ALWAYS A OBJECT
this.media_response = 'fake response2';
UploadService.media_response = 'fake response3';
if (angular.isDefined(data.id)) {
angular.forEach(data, function(value, key) {
// image[key] = value;
});
// $scope.post.linkThumbnail = false;
// $scope.post.video = '';
} else if (angular.isDefined(data.message)) {
// $scope.uploadError = data.message;
} else {
}
}, function(err) {
}, function (progress) {
// constant progress updates
this.progress = parseInt(100 * progress.loaded / progress.total) + '%';
}
);
};
return new UploadService();});
//控制器
UploadService.cordovaFileUpload(filename, url, targetPath, options);
console.log(UploadService); // to view in js console
您的 this.
变量的范围是最近的功能块,而不是 service/factory。
使用 var
或 let
在 service/factory 的根目录中创建变量。
abcdServices.service('UploadService', function($http, $localStorage, $location, $q, $rootScope, $cordovaFileTransfer) {
let upload_in_progress = false;
let progress = 0;
let media_response = '';
var UploadService = function() {
upload_in_progress = false;
progress = 0;
media_response = '';
};
UploadService.prototype.cordovaFileUpload = function (filename, url, targetPath) {
upload_in_progress = true;
media_response = 'no response';
// Rest of your code
};
return new UploadService();});
如果你真的need/want要使用this
,请使用新的函数箭头符号,它不会在函数块中创建新的this
-作用域。
abcdServices.service('UploadService', function(...) {
this.upload_in_progress = 'test';
this.progress = 0;
this.media_response = '';
someFunction = (param1, param2, ..) => {
console.log(this.upload_in_progress) // 'test'
}
});
我有 Angular 在移动应用程序中执行 Cordova 文件传输上传的服务 - 它将文件上传到媒体服务器并 returns 一个对象。我正在尝试将此对象传递给 media_response
属性 但我运气不好 - 我做错了什么?
请注意 - 我已经尝试使用 service
和 factory
,但似乎仍然没有任何乐趣 - $cordovaFileTransfer upload
块中的任何内容都不会传递给 class 一些奇怪的原因的属性。
我希望看到 media_response
具有与数据变量相同的输出,数据变量是从媒体服务器返回的对象,但它显示 no response
知道为什么我没有收到预期的回复吗?
// 预期响应
UploadService.media_response in the controller should be an object to match the console.log(data)
//实际响应
UploadService.media_response is the string 'no response'
//上传服务
abcdServices.service('UploadService', function($http, $localStorage, $location, $q, $rootScope, $cordovaFileTransfer) {
var UploadService = function() {
this.upload_in_progress = false;
this.progress = 0;
this.media_response = '';
};
UploadService.prototype.cordovaFileUpload = function (filename, url, targetPath) {
this.upload_in_progress = true;
this.media_response = 'no response';
var options = {
id: new Date() . getTime() + filename,
fileKey: "file",
fileName: filename,
chunkedMode: false,
mimeType: "multipart/form-data",
params : {'fileName': filename}
};
$cordovaFileTransfer.upload(url, targetPath, options).then(
function(result) {
// Success!
var data = JSON.parse(result.response);
console.log('file uploaded - response:', data); // ALWAYS A OBJECT
this.media_response = 'fake response2';
UploadService.media_response = 'fake response3';
if (angular.isDefined(data.id)) {
angular.forEach(data, function(value, key) {
// image[key] = value;
});
// $scope.post.linkThumbnail = false;
// $scope.post.video = '';
} else if (angular.isDefined(data.message)) {
// $scope.uploadError = data.message;
} else {
}
}, function(err) {
}, function (progress) {
// constant progress updates
this.progress = parseInt(100 * progress.loaded / progress.total) + '%';
}
);
};
return new UploadService();});
//控制器
UploadService.cordovaFileUpload(filename, url, targetPath, options);
console.log(UploadService); // to view in js console
您的 this.
变量的范围是最近的功能块,而不是 service/factory。
使用 var
或 let
在 service/factory 的根目录中创建变量。
abcdServices.service('UploadService', function($http, $localStorage, $location, $q, $rootScope, $cordovaFileTransfer) {
let upload_in_progress = false;
let progress = 0;
let media_response = '';
var UploadService = function() {
upload_in_progress = false;
progress = 0;
media_response = '';
};
UploadService.prototype.cordovaFileUpload = function (filename, url, targetPath) {
upload_in_progress = true;
media_response = 'no response';
// Rest of your code
};
return new UploadService();});
如果你真的need/want要使用this
,请使用新的函数箭头符号,它不会在函数块中创建新的this
-作用域。
abcdServices.service('UploadService', function(...) {
this.upload_in_progress = 'test';
this.progress = 0;
this.media_response = '';
someFunction = (param1, param2, ..) => {
console.log(this.upload_in_progress) // 'test'
}
});