我尝试解包一个承诺,以便在单击提交按钮时更新我的视图。在控制器中,我首先调用返回细节对象的getProfileDetails()
。这是控制器:
angular.module('duskWeb').controller('ProfileController', function(Profile) {
'ngInject';
var activate, getProfileDetails, passItemsToScope, vm;
vm = this;
activate = function() {
getProfileDetails();
};
getProfileDetails = function() {
return Profile.getDetails(userid).then(passItemsToScope);
};
vm.submit = function() {
return Profile.editDetails(updateParams).then(passItemsToScope);
};
passItemsToScope = function(data) {
vm.profileDetails = data;
};
activate();
});
在服务Profile
中,我使用Restangular来检索配置文件详细信息对象,以及使用post函数来更新配置文件详细信息。这是服务:
angular.module('duskWeb').service('Profile', function(API) {
var Profile;
return new (Profile = (function() {
function Profile() {
this.getDetails;
this.editDetails;
}
Profile.prototype.getDetails = function(id) {
var getDetailsComplete;
getDetailsComplete = function(response) {
return response.data;
};
return API.one('creator/' + id).get().then(getDetailsComplete, function(message) {
return console.log('Error: ' + message.status);
});
};
Profile.prototype.editDetails = function(params) {
var editDetailsComplete;
editDetailsComplete = function(response) {
toastr.success("Successfully updated profile");
return response.data;
};
return API.all('user').post({
email: params.email,
first_name: params.firstName,
last_name: params.lastName,
display_name: params.displayName,
location: params.location
}).then(editDetailsComplete, function(message) {
return console.log('Error: ' + message.status);
});
};
return Profile;
})());
});
以下是API服务的外观,再次使用Restangular:
angular.module('duskWeb').service('API', function(Restangular, toastr, $window, $state) {
return Restangular.withConfig(function(RestangularConfigurer) {
var headers, showToastr;
headers = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Methods': 'GET, POST',
'X-DuskFM-Token': ''
};
showToastr = function(message) {
toastr.info(message);
};
RestangularConfigurer.setBaseUrl('http://localhost:5050/v1')
.setDefaultHeaders(headers)
.setFullResponse(true)
.setResponseExtractor(function(response, method, endpoint) {
if (response.errors === false && method !== 'get') {
showToastr('Your request has been done successfully.');
}
return response;
}).setErrorInterceptor(function(response) {
var error;
if (response.status >= 400 && response.status <= 499) {
for (error in response.data.errors) {
return showToastr(response.data.errors[error][0]);
}
}
if (response.status === 500) {
return showToastr(response.statusText);
}
}).addFullRequestInterceptor(function(element, operation, what, url, headers) {
var token;
token = $window.localStorage.satellizer_token;
if (token) {
headers['X-DuskFM-Token'] = token;
}
});
});
});
现在,单击“提交”时视图不会更新。我想要发生的是在视图中调用vm.submit()时动态更新视图。知道为什么不发生这种情况吗?其他一切正常工作(配置文件更新,当我刷新页面时,我得到更新)。
谢谢。