0

我想在頁面加載時使用ANgularJs + TypeScript.I實現ng-optionsng-init方法來加載它,但我可能會遇到一些問題,這就是爲什麼我無法從我的API獲取我的數據或打電話給我的API。我已經給我的代碼提出了這個問題。Angular + TypeScript:如何獲取使用HTTP的方法Put

這裏是我的打字稿控制器: -

/// <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 它給「物權或簽名預期」 &意外令牌的錯誤。 「期望構造函數,方法,訪問器或屬性」。 Updated Error

+0

你有什麼錯誤?你只需要給我們所有的代碼而不需要解釋。很容易看到代碼中存在錯誤。例如:''http.get('./ Deskt ...'當你使用'[HttpPut]'時,不應該是'$ http.put'嗎?如果你想要一個好的答案解釋你的問題 –

+0

@ Antoine Esteve我已更新我的代碼以及是它提供了「預期的屬性或簽名」錯誤和意外的令牌。「構造函數,方法,訪問器或屬性預計爲」。\t 。 –

回答

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(... 
} 

這可能是上述打字稿控制器定義的運用一些更爲複雜的例子:

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編譯/轉譯器

+0

問題未解決請參閱更新錯誤圖像 –

+0

我添加了完整的代碼片段和一個鏈接到操場,在那裏你可以玩和檢查什麼和什麼不編譯/ transipler ... –

+0

Radim需要你的幫助http://stackoverflow.com/questions/31287908/angularjstypescript-how-to-get-selected-index-value-in-ng-select –