【问题标题】:Angular 1.x ES6 services $http undefined [closed]Angular 1.x ES6 服务 $http 未定义 [关闭]
【发布时间】:2015-12-17 08:24:17
【问题描述】:

我在 ES6 中使用 AngularJS,当我从我的服务中调用 register 函数时,出现错误:

this.$http is undefined

我的服务:

export default class AccountService {
  /* @ngInject */
  contructor($http) {
    this.$http = $http;
  }

  register(account) {
    return this.$http.post('/api/accounts/register', account).then(result => result.data);
  }
}

AccountService.$inject = ['$http'];

注册我的服务:

import AccountService from './services/accounts.service';
angular.module('app', [...])
  /* ... */
  .service('accountService', AccountService)

在我的控制器中:

export class RegisterController {
  /* @ngInject */
  constructor(accountService) {
    this.accountService = accountService;
  }

  register(account) {
    this.accountService.register(account);
  }
}

我使用生成器:https://github.com/Swiip/generator-gulp-angular

【问题讨论】:

    标签: javascript angularjs ecmascript-6


    【解决方案1】:

    您将constructor 拼错为contructor。它永远不会被调用,因此不会分配this.$http

    【讨论】:

    • 天哪,2 天 :(.
    猜你喜欢
    • 1970-01-01
    • 2017-07-16
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多