【问题标题】:Angular+TypeScript: How to get method using HTTP PutAngular+TypeScript:如何使用 HTTP Put 获取方法
【发布时间】:2015-09-16 23:28:40
【问题描述】:

我正在尝试使用 ANGularJs+TypeScript 在页面加载时获取我的所有下拉列表。我已经完成了实现 ng-optionsng-init 方法来加载它,但我可能会遇到一些问题,这就是为什么我无法获得我的来自我的 API 的数据或调用我的 API。我已经用这个问题给出了我的代码。

这是我的打字稿控制器:-

/// <reference path="../interface/interface.ts" />
/// <reference path="../../scripts/typings/jquery/jquery.d.ts" />
/// <reference path="../../scripts/typings/angularjs/angular.d.ts" />

module CustomerNew.controllers {
    export class CreateCustomerCtrl {
        static $inject = ['$scope', '$http', '$templateCache'];
        debugger;
        constructor(protected $scope: ICustomerScope,
            protected $http: ng.IHttpService,
            protected $templateCache: ng.ITemplateCacheService) {
            $scope.create = this.create;
            $scope.getFormData = this.getformData;
        }
        public getformData: (getFormData: any) => {
            $http.get();
        }

给出错误 AS 它给出了“预期的属性或签名”和意外令牌的错误。 “需要构造函数、方法、访问器或属性”。

【问题讨论】:

  • 你有什么错误?您只需将所有代码提供给我们,无需解释。很容易看出您的代码中有错误。例如:$http.get('./Deskt... 当你使用[HttpPut] 时,不应该是$http.put 吗?如果您想要一个好的答案,请解释您的问题。
  • @Antoine Esteve 我也更新了我的代码,是的,它给出了“预期属性或签名”和意外令牌的错误。 “需要构造函数、方法、访问器或属性”。 .

标签: angularjs angularjs-scope typescript typescript1.4 typescript1.5


【解决方案1】:

我们需要this.和“=”来分配getformData

 constructor(protected $scope: ICustomerScope,
        protected $http: ng.IHttpService,
        protected $templateCache: ng.ITemplateCacheService) {
        $scope.create = this.create;
        $scope.getFormData = this.getformData;
 }
 //public getformData: (getFormData: any) => {
 public getformData = (getFormData: any) => {
    // instead of this notation
    // $http.get(...
    // we need to use this.
    this.$http.get(...
 }

这可能是上述 TypeScript 控制器定义的一些更复杂的示例:

module CustomerNew.controllers
{
    export interface ICustomerScope extends ng.IScope
    {
        create: Function;
        getFormData: Function;
    }
    export class CreateCustomerCtrl {
        static $inject = ['$scope', '$http', '$templateCache'];
        debugger;
        constructor(protected $scope: ICustomerScope,
            protected $http: ng.IHttpService,
            protected $templateCache: ng.ITemplateCacheService) {
            $scope.create = this.create;
            $scope.getFormData = this.getformData;
        }
        public getformData = (getFormData: any) =>
        {
            this.$http.get("url"); // ...
        }
        public create = () => { }
    }
}

在这个typescript playground中检查它编译器/转译器

【讨论】:

  • 问题未解决,请查看更新后的错误图片
  • 我添加了完整的代码 sn-p 和一个指向 playground 的链接,您可以在其中玩并检查什么会编译/transipler...
  • 嗨,Mico...我会在白天检查它...但我不能保证我会知道如何...只是为了确保您了解我的局限性...; )
  • @Mico 我试图让你的场景工作,希望它有点帮助......现在必须出去......
猜你喜欢
  • 2021-12-22
  • 2014-12-23
  • 2015-08-16
  • 2019-06-17
  • 1970-01-01
  • 2016-03-30
  • 2018-02-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多