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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,33 @@ var Error = window.Error,
push = [].push,
toString = Object.prototype.toString,


_angular = window.angular,
/** @name angular */
angular = window.angular || (window.angular = {}),
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.
*
* @returns {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 @@ -632,4 +632,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();
});

});

});