我一直在使用AngularJS摸索數週,並試圖將這部分內容與其他內容一起拼湊起來,以創建文件服務Web應用程序與Django後端。我認爲事情進展順利,直到我發現自己試圖上傳帶有我所有其他表單數據的文件。我的HTML表單始終顯示爲在發送請求之前在驗證步驟中沒有附加任何文件。那麼,這不好!無論如何,這最終成爲某種不支持的操作方式,出於某種原因。我轉向ng-file-upload
,這是AngularJS的第三方文件上傳服務。 ng-file-upload
的最新迭代使用AngularJS 1.6風格的請求,我的第三方註冊應用angular-django-registration-auth
使用1.677之前的$http
。AngularJS:使用django後端將http從1.5.x遷移到1.6+
我需要更新第三方註冊應用程序,但它具有以下代碼。
'request': function(args) {
// Let's retrieve the token from the cookie, if available
if($cookies.token){
$http.defaults.headers.common.Authorization = 'Token ' + $cookies.token;
}
// Continue
params = args.params || {}
args = args || {};
var deferred = $q.defer(),
url = this.API_URL + args.url,
method = args.method || "GET",
params = params,
data = args.data || {};
// Fire the request, as configured.
$http({
url: url,
withCredentials: this.use_session,
method: method.toUpperCase(),
headers: {'X-CSRFToken': $cookies['csrftoken']},
params: params,
data: data
})
.success(angular.bind(this,function(data, status, headers, config) {
deferred.resolve(data, status);
}))
.error(angular.bind(this,function(data, status, headers, config) {
console.log("error syncing with: " + url);
// Set request status
if(data){
data.status = status;
}
if(status == 0){
if(data == ""){
data = {};
data['status'] = 0;
data['non_field_errors'] = ["Could not connect. Please try again."];
}
// or if the data is null, then there was a timeout.
if(data == null){
// Inject a non field error alerting the user
// that there's been a timeout error.
data = {};
data['status'] = 0;
data['non_field_errors'] = ["Server timed out. Please try again."];
}
}
deferred.reject(data, status, headers, config);
}));
return deferred.promise;
},
在var deferred =
開始(這是定義一個承諾對象,對不對?)我對正在發生的事情不清楚。這些任務大部分都很容易理解,除了授予承諾對象外(data = args.data || {};
最終如何在$http
其中一個提供者的複合分配中右移?),但是success
和error
案件中究竟發生了什麼? angular.bind()
被調用?我似乎無法找到任何角度似乎與承諾相關的好例子。