Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(noConflict): restore previous namespace #1535

Closed
wants to merge 5 commits into from
Closed

feat(noConflict): restore previous namespace #1535

wants to merge 5 commits into from

Conversation

treasonx
Copy link
Contributor

@treasonx treasonx commented Nov 6, 2012

Angular needs to provide a way to restore the previously defined angular global namespace to prevent conflict with existing scripts. This is especially important if there are multiple versions of angular loaded onto the same page. If angular is used in a thirdparty javascript environment it should provide a way to clean up the global after loading.

This feature is similiar to the jQuery noConflict function.

@@ -58,6 +58,8 @@ var Error = window.Error,
push = [].push,
toString = Object.prototype.toString,


_angular = window.angular,
/** @name angular */
angular = window.angular || (window.angular = {}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tried your patch, but looking at this line, it seems Angular will reuse any existing window.angular and extend it. This goes directly against the principle of returning the old value unchanged, and in fact turns noConflict() into a no-op.

@treasonx
Copy link
Contributor Author

treasonx commented Nov 7, 2012

@mernen you were correct about the noop I have updated the pull request. I would like to add a spec for this.. In order to test I would like to load 2 versions of angular on the page and then test to make sure they are different objects. I'm not exactly sure where in the testing framework I can add another angular script tag during a test run. I would dynamically load the angular script in the spec, but im not sure thats the best approach.

@mhevery
Copy link
Contributor

mhevery commented Jan 18, 2013

Thanks for your contribution! In order for us to be able to accept it, we ask you to sign our CLA (contributor's license agreement).

CLA is important for us to be able to avoid legal troubles down the road.

For individuals (a simple click-through form):
http://code.google.com/legal/individual-cla-v1.0.html

For corporations (print, sign and scan+email, fax or mail):
http://code.google.com/legal/corporate-cla-v1.0.html

@mhevery mhevery closed this Jan 18, 2013
@mhevery mhevery reopened this Jan 18, 2013
@ghost ghost assigned mhevery Jan 18, 2013
@treasonx
Copy link
Contributor Author

@mhevery i just signed the CLA

@mhevery
Copy link
Contributor

mhevery commented Feb 14, 2013

MERGED, thanks

@mhevery mhevery closed this Feb 14, 2013
@chrisdew
Copy link

Does this mean noConflict is in a released version of AngularJS?

@rnikitin
Copy link

I don't see noConflict in production angular file.

@GlennKirbo
Copy link

Very sad to see this feature missing from production. This is an important requirement for API vendors who use Angular.

@snostorm
Copy link

snostorm commented Dec 5, 2013

It looks like this was removed (for some reason) in 9faabd1 which is sad since I was looking to create a self contained embeddable app. A hack can be done by creating a custom wrapper which sets window.angular to null (after backing up any existing instance), loading in angular, then restoring window.angular to the original reference afterward.

@johntday
Copy link

so where do we stand with this?

@brandonburkett
Copy link

I also have need to build widgets to be used by third party sites and am interested in not having our version of angular conflict with the host site's version. Is an official noConflict() in the works?

@caitp
Copy link
Contributor

caitp commented Aug 28, 2014

#8596 (won't land in its current form)

@treasonx
Copy link
Contributor Author

@devourment77 I worked around the lack of noConflict by isolating Angular in a same domain iframe. Then I manually bootstrapped my angular widgets.

I used angular as a UI framework for a thirdparty ad network. Angular couldn't be introduced globally on the publisher's pages so the iframe provided the needed isolation.

The widgets were not rendered in the iframe. They were rendered on the host page, but a same domain can access the DOM of the parent page. So angular could bootstrap the widgets if I provided the element.

@brandonburkett
Copy link

@treasonx Thanks for the idea. Right now I am trying to following this method: http://www.mattburkedev.com/multiple-angular-versions-on-the-same-page/ by for whatever reason, the closure version of angular is still creeping out to the main DOM.

Did you run into any of the JSONP issues?

@treasonx
Copy link
Contributor Author

@devourment77 we are getting off topic this will be my last reply about iframes here:

I didn't use anything besides the bindings and directives. I would assume that JSONP would just work since the script tags created by angular would be in the context of the iframe.

Look at lightningjs they have a robust way of creating the same domain iframe. I used them as inspiration when I was creating my same domain iframe

If you are writing third-party js a hidden same domain iframe provides the best isolation! I would never write third-party js without one!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants