Coder Social home page Coder Social logo

christopherthielen / angular2react Goto Github PK

View Code? Open in Web Editor NEW

This project forked from coatue-oss/angular2react

0.0 1.0 1.0 273 KB

One line of code to turn any Angular 1 Component into a React Component

License: Other

TypeScript 90.43% JavaScript 9.57%

angular2react's Introduction

Angular to React: The easiest way to embed Angular components in a React app

angular2react Build Status NPM Apache2

One line of code to turn any Angular 1 Component into a React Component (opposite of react2angular)

Installation

npm install angular2react --save

Usage

1. Save a reference to the $injector

let $injector
angular
  .module('myModule')
  .run(['$injector', function(_$injector) { $injector = _$injector }])

2. Create an Angular component

const MyComponent = {
  bindings: {
    fooBar: '<',
    baz: '<'
  },
  template: `
    <p>FooBar: {this.$ctrl.fooBar}</p>
    <p>Baz: {this.$ctrl.baz}</p>
  `
}

3. Expose it to Angular

angular
  .module('myModule', [])
  .component('myComponent', MyComponent)

4. Convert it to a React Component

import { angular2react } from 'angular2react'

const MyComponent = angular2react('myComponent', MyComponent, $injector)

5. Use it in your React code

<MyComponent fooBar={3} baz='baz' />

Why step 1?

We need a reference to the $injector created by the Angular module that registered the Angular component you're exposing. That way we can manually compile your component.

If you use ngimport, you can skip step 1 and omit the last argument in step 4:

import { angular2react } from 'angular2react'

const MyComponent = angular2react('myComponent', MyComponent)

Caveats

  • Only one way bindings (<) are supported, because this is the only type of binding that React supports
  • Be sure to bootstrap your Angular app before rendering its React counterpart

Tests

npm test

License

Apache-2.0

angular2react's People

Contributors

bcherny avatar greenkeeper[bot] avatar jrsquared avatar

Watchers

 avatar

Forkers

shrinathgupta

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.