【问题标题】:Angular + TypeScript + AMD = duplication?Angular + TypeScript + AMD = 重复?
【发布时间】:2015-09-17 10:18:36
【问题描述】:

我已经在 Angular 中进行了几个月的开发,现在正在调查是否值得将我的开发迁移到 TypeScript。我遇到的问题之一是使用外部模块和注入服务时的重复。例如我有这个(简化的)代码:

///<reference path="../../../../typings/tsd.d.ts" />

// imports for strong typing
import audioModel = require('app/plugins/audio/audio.model');

export class AudioController {

    public model: audioModel;

    static $inject = ['$scope', '$q', 'AudioModel'];
    constructor(protected $scope, protected $q, protected AudioModel:audioModel) {
        this.model = new AudioModel();
    }

}

AudioModel 是在外部模块“audio.model.ts”中定义的 Angular 工厂,所以在我看来,我注入了两次——一次使用 Angular,再次使用TypeScript 导入以获得强类型。

有没有更好的方法在 AMD 项目中同时使用 Angular 依赖注入和强类型?

【问题讨论】:

    标签: angularjs typescript


    【解决方案1】:

    有没有更好的方法在 AMD 项目中同时使用 Angular 依赖注入和强类型

    这是 Angular 1.x 依赖注入的问题。 Angular 模块系统独立于现有的传统浏览器模块系统(例如 AMD)。

    所以你确实需要导入两次。一旦类型,接下来是 DI。

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 2012-10-10
      • 1970-01-01
      • 2015-08-29
      • 2016-07-29
      • 2013-02-22
      • 2012-11-29
      • 2013-04-30
      • 2021-08-07
      相关资源
      最近更新 更多