0

我已经使用 generator-angular-fullstack 和一个新制作的路由客户端/应用程序/登录。它包含 login.controller.js、login.html、login.js、login.controller.spec.js 和 login.scss。我已经设置了一个登录 API 端点,我想从它返回到 cookie 的 JSON 中保存“authentication_token”和“user_id”。Angular-cookies (ngCookies) 已安装。

我已经尝试调整文档示例(https://docs.angularjs.org/api/ngCookies/service/$cookies),但到目前为止运气不佳。控制台错误 = '$cookies.put 不是函数' (Angular 1.5.6)。还有,变

angular.module('myApp');

angular.module('myApp', ['ngCookies']); 

甚至

angular.module('myApp', ['']); 

已经使登录页面变为空白,但标题除外。我认为模块不存在无关紧要,因为尝试加载不存在的模块会给出一个完全空白的页面(也没有标题)。

有什么建议么?

登录.html

<body>
<script src="/bower_components/angular-cookies/angular-cookies.js"></script>    
<form ng-submit="callSignIn(username, password, site)">
Email:
  <input type="text" ng-model="username"><br>
Password:
  <input type="text" ng-model="password"><br>
Site:
  <input type="text" ng-model="site"><br>
  <input type="submit" value= "Submit">
</form>
<section ui-view>Response: {{response}}</section>
</body>

login.controller.js

'use strict';
(function(){

  class LoginComponent {
    constructor($scope, $http, $cookies) {
      $scope.callSignIn = function(username, password, site) {
        $http.post('/api/auth/signin', {
          username: username,
          password: password,
          site: site
        }).success(function(response, $cookies) {
          $scope.response = response;
          $cookies.put('myFavorite', 'oatmeal'); //testvalue
        });
      };

    }
  }

  angular.module('orbitApp') //angular.module('orbitApp', ['ngCookies']) gives blank login page...
    .component('login', {
      templateUrl: 'app/login/login.html',
      controller: LoginComponent
    });

})();

登录.js

'use strict';

angular.module('orbitApp')
  .config(function ($stateProvider) {
    $stateProvider
      .state('login', {
        url: '/login',
        template: '<login></login>'
      });
  });

典型的 JSON 响应:

{"http_code":200,"response":{"authentication_token":"a","site_id":"b","content_url":"c","user_id":"d"}}
4

2 回答 2

1

为什么要加载angular-cookies.jslogin.html 而不是 index.html。

当您的模块引导时,您angular-cookies无法使用它使其变为空白,您可以拥有一个console error too.

于 2016-06-06T11:07:31.243 回答
0

通过改变解决

.success(function(response, $cookies) {

.success(function(response) {

同时保持其他一切相同(包括 angular.module)。它现在就像一个魅力。

于 2016-06-06T14:10:49.990 回答