如何在打字稿中 angularjs app.service 和 $q

How to angularjs app.service and $q in typescript

我对打字稿和 angular Js 很陌生。我找不到正确的答案,我的代码如下:

export class SidenavController {
static $inject = ['$scope', '$mdSidenav'];
     constructor(private $scope: any,private $mdSidenav: any) {

     }
toggleSidenav(name: string) {
    this.$mdSidenav(name).toggle();
     }
  loadHelpInfo() {
    this.helpService.loadAll()
      .then(function(help) {
        this.$scope.helpInfo = [].concat(help);
        this.$scope.selected = this.$scope.helpInfo[0];
      })
  }
  selectHelpInfo(help) {
    this.$scope.selected = angular.isNumber(help) ? this.$scope.helpInfo[help] : help;
    this.$scope.toggleSidenav('left');
  } 

}
app.service('helpService', ['$q', function($q) {
  var helpInfo = [{
      name: 'Introduction',
      content: '1 '
  }, {
      name: 'Glossary',
      content: '2'
  }, {
      name: 'Log In',
      content: '3'
      }, {
      name: 'Home Page',
      content: '4'
  }];

  return {
      loadAll: function() {
          return $q.when(helpInfo);
      }
  };
}]);

在上面的代码中,我想使用 helpService 在屏幕上加载详细信息。 执行时出现以下错误: app/components/sidenav/sidenav-controller.ts(10,10):错误 TS2339:属性 'helpService' 在类型 'SidenavController' 上不存在。 我不确定如何在打字稿中使用服务。 另外,如果需要,我已经完成了 angular 的 codepen 版本:

http://codepen.io/snehav27/pen/JdNvBV

基本上我正在尝试做上面片段的打字稿版本

您需要注入 helpservice 并将其设置在构造函数参数中。

     static $inject = ['$scope', '$mdSidenav', 'helpService'];
     constructor(private $scope: any,private $mdSidenav: any, private helpService:any) {

     }

否则 Typescript 不知道您指的是什么 this.helpService,即使没有 TS,当您尝试使用 "cannot access loadAll of undefined" 错误或类似的方法执行 this.helpService.loadAll 时也会导致错误.

同样使用箭头运算符解析词法作用域 this @

  this.helpService.loadAll()
  .then((help) => { //<-- here
    this.$scope.helpInfo = [].concat(help);
    this.$scope.selected = this.$scope.helpInfo[0];
  });

否则会导致另一个错误,因为 this 不会是回调中的控制器实例。

您还可以为 helpService 创建一个打字,以便更好地使用并减少任何拼写错误。

export interface IHelpService{
  loadAll():ng.IPromise<Array<HelpInfo>>; //use specific typing instead of any
}

export interface HelpInfo{
  name:string;
  content:string;
}

class HelpService implements IHelpService{
    private _helpInfo:Array<HelpInfo> = [{
      name: 'Introduction',
      content: '1 '
  }, {
      name: 'Glossary',
      content: '2'
  }, {
      name: 'Log In',
      content: '3'
      }, {
      name: 'Home Page',
      content: '4'
   }];

    static $inject = ['$q'];
    constructor(private $q:ng.IQService){
    }

    loadAll(){
       return this.$q.when(this._helpInfo);
    }

 }

angular.module('HelpApp').service('helpService', HelpService);

 constructor(private $scope: any,private $mdSidenav: any, private helpService:IHelpService) {

最好将控制器用作 Typescript class defn 的语法,并完全摆脱将属性附加到范围。您现在拥有的是半成品(功能附加到控制器实例和一些属性范围)。

export class SidenavController {

    helpInfo:Array<HelpInfo>;
    selected:HelpInfo; 

    static $inject = ['$mdSidenav', 'helpService'];
    constructor(private $mdSidenav: any, private helpService:IHelpService) {}

     toggleSidenav(name: string) {
        this.$mdSidenav(name).toggle();
     }

     loadHelpInfo() {
        this.helpService.loadAll()
          .then((help) => {
            this.helpInfo = [].concat(help);
            this.selected = this.helpInfo[0];
          })
      }

      selectHelpInfo(help) {
        this.selected = angular.isNumber(help) ? this.helpInfo[help] : help;
        this.toggleSidenav('left');
      } 

}

在您看来:

<body layout="column" ng-controller="AppCtrl as vm">

并引用前缀为vm的属性和方法(你可以使用任何别名,我只使用vm)。示例(你应该能够弄清楚其余的):-

<md-item ng-repeat="it in vm.helpInfo">

<--- some code -->

 <md-button ng-click="vm.selectHelpInfo(it)" 
            ng-class="{'selected' : it === vm.selected }">