Skip to content

Commit

Permalink
feat(core/reactShims): Add angularJS template/controller adapter (#4730)
Browse files Browse the repository at this point in the history
* feat(core/reactShims): Add angularJS template/controller adapter
  • Loading branch information
christopherthielen authored and Justin Reynolds committed Jan 26, 2018
1 parent 5e48a3c commit b183954
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions app/scripts/modules/core/src/reactShims/AngularJSAdapter.tsx
@@ -0,0 +1,52 @@
import * as React from 'react';
import * as angular from 'angular';
import { IScope } from 'angular';
import { $compile, $controller } from 'ngimport';

export interface IRenderAngularJSProps {
template: string;
controller: string;
locals?: { [key: string]: any };
}

export class AngularJSAdapter extends React.Component<IRenderAngularJSProps> {
public static defaultProps: Partial<IRenderAngularJSProps> = { locals: {} };
private $scope: IScope;

constructor(props: any) {
super(props);
}

public componentWillReceiveProps(nextProps: IRenderAngularJSProps) {
this.$scope.props = nextProps;
}

private refCallback(ref: Element) {
if (!ref) {
return;
}

const { template, controller, locals } = this.props;
const $element = angular.element(ref);

const parentScope = $element.scope();
const $scope = this.$scope = parentScope.$new();
$scope.props = this.props;

$element.html(template);
$compile(ref)($scope);

if (controller) {
const controllerInstance = $controller(controller, { $scope, $element, ...locals });
$element.data('$ngControllerController', controllerInstance);
}
}

public componentWillUnmount() {
this.$scope.$destroy();
}

public render() {
return <div ref={(ref) => this.refCallback(ref)} />
}
}

0 comments on commit b183954

Please sign in to comment.