使用下面的代码片段我正在验证电子邮件和密码。 customerlogin()
方法返回一些我想在下一页中显示的 JSON 数据。换句话说,我想将从 customerlogin()
返回的数据传递给 then()
,然后将其传递给 /customerprofile
请帮忙
login(form) {
this.submitted = true;
if (form.$valid) {
this.Auth.customerlogin({
email: this.operator.email,
password: this.operator.password
})
.then(() => {
// Logged in, redirect to home
this.$location.path('/customerprofile');
})
.catch(err => {
this.errors.login = err.message;
});
}
}
//其他文件Auth.js
customerlogin({
email,
password
}, callback) {
console.log('Customer Authentice Method');
return $http.post(properties.customer_login, {
email, password
})
.then(res => {
properties.GetId = res.data.id;
$cookies.put('token', res.data.token);
currentUser = User.get();
return currentUser.$promise;
})
.then(user => {
safeCb(callback)(null, user);
return user;
})
.catch(err => {
Auth.logout();
safeCb(callback)(err.data);
return $q.reject(err.data);
});
}
我想在这些文本框中显示数据 enter image description here
最佳答案
您的登录函数应该调用一个服务方法,该方法进行 ajax 调用并将响应存储为该服务上的对象属性。然后 Controller 将其纳入范围,因为您已经注入(inject)了服务。没有什么可以通过的。它已经在那里并且由 Angular 自动监视。
类似这样的事情:
angular.someModule('someModule')
.service('someService', function($http) {
return {
loginCall: function(...) {
// do ajax call here
return loginStuff; // must be an object (or wrapped in one)
}
};
})
.controller('SomeController', ['someService', function(someService) {
var sc = this; // controllerAs syntax
sc.login = function(form) {
someService.customerlogin(...).then(...).catch(...);
// because someService has been injected, someService.loginCall is now
// available and being watched by the controller, on its scope...
// and can be used in your view like {{someService.loginCall.dataProperty}}
...
};
}]);
这里可能缺少一些部分(模块注入(inject)),但这应该可以帮助您开始。
关于javascript - 如何在其他页面AngularJS中显示响应数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40550486/