使用下面的代码段我正在验证电子邮件,密码。 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
答案 0 :(得分:4)
您的登录功能应该调用一个服务方法,该方法进行ajax调用并将响应存储为该服务的对象属性。然后控制器具有该范围,因为您已注入该服务。没有什么可以通过的。它已经存在,并由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}}
...
};
}]);
这里可能有一些缺失的部分(模块注射),但这应该让你开始。
答案 1 :(得分:1)
首先,尝试将此结构用于.then
:
.then(function (data) {
$log.debug(data); //to console log passed data via angular
});