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

时间:2015-06-09 19:44:30

标签: angularjs typescript angular-material

我是打字稿和角度J的新手。我无法找到正确的答案,我的代码如下:

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:类型'SidenavController'上不存在属性'helpService'。 我不知道如何在打字稿中使用服务。 如果需要,我也完成了角度的codepen版本:

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

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

1 个答案:

答案 0 :(得分:1)

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

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

     }

否则Typescript不知道你所指的是什么this.helpService,即使没有TS,当你尝试this.helpService.loadAll使用“无法访问loadAll of undefined”错误或类似错误时会导致错误

还可以使用箭头运算符来解析词法作用域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类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 }">