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

Commit

Permalink
feat(noConflict): restore previous angular namespace reference
Browse files Browse the repository at this point in the history
  • Loading branch information
treasonx authored and mhevery committed Feb 14, 2013
1 parent b7e1fb0 commit 12ba6ce
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,32 @@ var /** holds major version number for IE or NaN for real browsers */
push = [].push,
toString = Object.prototype.toString,


_angular = window.angular,
/** @name angular */
angular = window.angular || (window.angular = {}),
angularModule,
nodeName_,
uid = ['0', '0', '0'];

/**
* @ngdoc function
* @name angular.noConflict
* @function
*
* @description
* Restores the previous global value of angular and returns the current instance. Other libraries may already use the
* angular namespace. Or a previous version of angular is already loaded on the page. In these cases you may want to
* restore the previous namespace and keep a reference to angular.
*
* @return {Object} The current angular namespace
*/
function noConflict() {
var a = window.angular;
window.angular = _angular;
return a;
}

/**
* @ngdoc function
* @name angular.forEach
Expand Down
3 changes: 2 additions & 1 deletion src/AngularPublic.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function publishExternalAPI(angular){
'isDate': isDate,
'lowercase': lowercase,
'uppercase': uppercase,
'callbacks': {counter: 0}
'callbacks': {counter: 0},
'noConflict': noConflict
});

angularModule = setupModuleLoader(window);
Expand Down
23 changes: 23 additions & 0 deletions test/AngularSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,4 +737,27 @@ describe('angular', function() {
expect(toJson({key: $rootScope})).toEqual('{"key":"$SCOPE"}');
}));
});

describe('noConflict', function() {
var globalAngular;
beforeEach(function() {
globalAngular = angular;
});

afterEach(function() {
angular = globalAngular;
});

it('should return angular', function() {
var a = angular.noConflict();
expect(a).toBe(globalAngular);
});

it('should restore original angular', function() {
var a = angular.noConflict();
expect(angular).toBeUndefined();
});

});

});

0 comments on commit 12ba6ce

Please sign in to comment.