4

Is it possible to create an Angular app inside a react component?

Something like:

React.createClass({
    render: function() {
        return <div ng-app>...</div>;
    }
});

This is completely backwards, but will only have to be a temporary solution. I'm assuming I could do something like the code above, and add the angular bootstrapping to the componentDidMount lifecycle method.

Has anyone successfully done this?

Thanks.

1 Answer 1

4

It would look something like this:

componentDidMount: function(){
    this._angularEl = document.createElement("div");
    this._angularEl.innerHTML = angularTemplatHTMLString;
    this._angularEl.setAttribute("ng-app", "");
    this.getDOMNode().appendChild(this._angularEl);
    // bind angular to this._angularEl
},
componentWillUnmount: function(){
    // unbind angular from this._angularEl

    this.getDOMNode().removeChild(this._angularEl);
    delete this._angularEl;
},
render: function(){
    return <div></div>
}

You could either use this for each component, or create a function which returns a mixin.

function makeAngularMixin(template){
    return { /* above code except for render */ }
}

or have a component which allows passing an angular template via props.

Sign up to request clarification or add additional context in comments.

1 Comment

What would be inside angularTemplatHTMLString?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.