Skip to content

Instantly share code, notes, and snippets.

@brigand
Last active August 29, 2015 13:56
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brigand/9320091 to your computer and use it in GitHub Desktop.
Save brigand/9320091 to your computer and use it in GitHub Desktop.
react components in an angular directive
angular.module('app.common.directives.html-editor', [])
.directive('htmlEditor', function() {
return {
restrict: 'E',
scope: {
'html': '='
},
link: function (scope, element, attrs, ctrl) {
// this is a browserify bundle where my react components live
var HtmlEditor = require("app-common").HtmlEditor;
// I have a single file with `module.exports = window.React; delete window.React;` aliased as 'react'
var React = require('react');
// give our component a home
var angularElement = angular.element("<div></div>");
element.append(angularElement);
var reactElement = angularElement[0];
// HtmlEditor is created with React.createClass, so we're just passing in some props
var component = HtmlEditor({
html: scope.html,
onChange: function(e, html){
// tell angular we changed something
scope.html = html;
scope.$apply();
}
});
// work with angular to manage the life cycle of our component
React.renderComponent(component, reactElement);
// clean up, our componentWillUnmount calls and everyone's happy
scope.$on("$destroy", function(){
React.unmountComponentAtNode(reactElement);
});
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment