2016-11-11 48 views
2

使用下面的代码段我正在验证电子邮件,密码。 customerlogin()方法返回一些我想在下一页显示的JSON数据。换句话说,我想从customerlogin()返回的数据传递给then(),然后把它传递给/customerprofile 请帮如何在其他页面显示响应数据angularjs

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

+1

'然后把它传递给/ customerprofile'。你想传递一些数据给**另一个**控制器? –

+0

是的..我想要显示我的json数据在html页面使用angularjs –

回答

4

您的登录函数应该调用一个服务方法,该方法使ajax调用并将响应存储为对象pro对这项服务很感兴趣。控制器然后在范围内具有该范围,因为您已经注入了该服务。没有什么可以传递的。它已经在那里,并由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}} 
     ... 
    }; 
}]); 

可能有一些缺件在这里(模块注射),但这应该让你开始。

+0

{“_id”:“5825978738c03e3c187cd006”,“full_name”:“阿迪汗”,“电子邮件”:“[email protected]”}我想这在那么, –

+0

如何获得角度页面客户资料中的数据 –

+0

可否请您举例代码,我的英文不太好,很难理解 –

1

首先,尝试使用这种结构为您.then

.then(function (data) { 
     $log.debug(data); //to console log passed data via angular 
    }); 
相关问题