0

我正在尝试使用 AAngularJs+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 它给出了“预期的属性或签名”和意外令牌的错误。“需要构造函数、方法、访问器或属性”。 更新错误

4

1 回答 1

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 = () => { }
    }
}

在这个打字稿操场 编译器/转译器中检查它

于 2015-06-30T09:10:03.943 回答