百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

ngimport

> 前端框架
开源

易于使用的 ES6 引入 $http、$log 以及其他 Angular 1 服务

100 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

易于使用的 ES6 引入 $http、$log 以及其他 Angular 1 服务

ngimport Build Status npm mit

ES6 imports for $http, $log, and other Angular 1 services

Install

bash
# Using Yarn:
yarn add ngimport angular

# Or, using NPM:
npm install ngimport angular --save

If you're using ngResource, also install ngimport-ngresource.

Example

Note: This example is in TypeScript, but it works equally well in plain JavaScript.

Before:

typescript
import {IHttpService, ILogService, IPromise} from 'angular'

angular.factory('Get', function($http: IHttpService, $log: ILogService) {
  return function(url: string): IPromise {
    return $http.get(url).then(data => {
      $log.info('Got data!', data)
      return data
    })
  }
})

export interface Get {
  (url: string): IPromise
}

After:

typescript
import {IPromise} from 'angular'
import {$http, $log} from 'ngimport'

export function Get(url: string): IPromise {
  return $http.get(url).then(data => {
    $log.info('Got data!', data)
    return data
  })
}

Full Example

Before:

…

After:

typescript
angular.module('myModule', ['bcherny/ngimport'])

// Contents of Get.ts:

import {IPromise} from 'angular'
import {$http, $log} from 'ngimport'

export function Get(url: string): IPromise {
  return $http.get(url).then(data => {
    $log.info('Got data!', data)
    return data
  })
}

// Contents of MyComponent.ts:

import {Get} from './Get'

angular.module('myModule').component('MyComponent', {
  controller: class MyComponentController {
    get() {
      Get('/foo').then(data => ...)
    }
  }
})

Why?

Angular 1 DI made sense when there was no JavaScript module standard. But with the advent of CommonJS, and now ES Modules, Angular DI only makes your code less portable.

If you add TypeScript to the mix, you'll often find yourself repeating class interface definitions: you might create a typed service class, but because its dependencies are injected via a closure, you can't export the class directly, and instead need to create a second interface and export it instead! And if you use the class' constructor to inject dependencies, then you can't pass arguments to a new instance of your constructor!

With the ngimport approach, all of these issues are solved.

But the biggest benefit is your code becomes much more portable: you can mix and match Angular 1, Angular 2, or even React components with zero friction. And if you're using TypeScript, you can do all of this in a 100% typesafe way.

Upsides of this approach

  • No more ugly, proprietary DI! Use standard imports
  • No lock in: easy migration path to Angular2, React, etc.
  • Use constructors to pass in arguments, rather than for DI
  • Avoid duplicated TypeScript interface declarations
  • Mock Angular dependencies with $provide in your unit tests, as usual
  • Assert against HTTP requests with $httpBackend in your unit tests, as usual
  • Use it as an adapter to migrate your codebase to imports piece by piece

Using this technique to wrap your own legacy modules

You can easily use the same technique that ngimport uses to expose your own, legacy Angular 1 modules via ES6 imports. Let's say you have the following code:

javascript
// Contents of myModule.js:

angular
  .module('myModule', [])
  .service('fooService', function($http) {
    this.foo = function() {
      return $http.get('/url')
    }
  })

To consume fooService today, you need to DI it; instead, let's expose it and its typings so we can import it:

typescript
// Contents of fooService.ts:

import {IPromise, module} from 'angular'
export let fooService = undefined

interface FooService {
  foo: () => IPromise
}

module('myModule').run(function ($injector) {
  fooService = $injector.get('fooService')
})

Voila! Now instead of DIing fooService, we can now simply write import {fooService} from './fooService'. We then have the freedom to migrate fooService to TypeScript/ES6 at our own pace.

Limitations

  • Angular builtins ($http, $rootScope) will be undefined until you bootstrap your app. This is due to the way Angular creates injectors. Be careful to either not use these builtins at the top level, or bootstrap the app before you do.
  • If transpiling to CommonJS, be careful to destructure the import rather than importing a default value. Otherwise when the exported reference updates, your consumer will still have a pointer to the old, undefined reference.

License

MIT

Running the tests

npm test

Todo

  • Add support for $animate, $animateCss, $aria, $cookies, $provide, $resource, $rootRouter, $route, $routeParams, $routerRootComponent, $sanitize, $swipe, $touch

Issues· 2 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptangulares6typescript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架