This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
feat($compile): isolate scope properties in controller context via controllerAs #7645
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,13 +68,24 @@ function $ControllerProvider() { | |
* It's just a simple call to {@link auto.$injector $injector}, but extracted into | ||
* a service, so that one can override this service with [BC version](https://gist.github.com/1649788). | ||
*/ | ||
return function(expression, locals) { | ||
return function(expression, locals, later, ident) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the extra parameters are undocumented as they're not intended for public use, but basically what they are:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add this as private comment into the code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you mean in addition to the big comment on lines 90-98? |
||
// PRIVATE API: | ||
// param `later` --- indicates that the controller's constructor is invoked at a later time. | ||
// If true, $controller will allocate the object with the correct | ||
// prototype chain, but will not invoke the controller until a returned | ||
// callback is invoked. | ||
// param `ident` --- An optional label which overrides the label parsed from the controller | ||
// expression, if any. | ||
var instance, match, constructor, identifier; | ||
later = later === true; | ||
if (ident && isString(ident)) { | ||
identifier = ident; | ||
} | ||
|
||
if(isString(expression)) { | ||
match = expression.match(CNTRL_REG), | ||
constructor = match[1], | ||
identifier = match[3]; | ||
identifier = identifier || match[3]; | ||
expression = controllers.hasOwnProperty(constructor) | ||
? controllers[constructor] | ||
: getter(locals.$scope, constructor, true) || | ||
|
@@ -83,19 +94,51 @@ function $ControllerProvider() { | |
assertArgFn(expression, constructor, true); | ||
} | ||
|
||
instance = $injector.instantiate(expression, locals, constructor); | ||
if (later) { | ||
// Instantiate controller later: | ||
// This machinery is used to create an instance of the object before calling the | ||
// controller's constructor itself. | ||
// | ||
// This allows properties to be added to the controller before the constructor is | ||
// invoked. Primarily, this is used for isolate scope bindings in $compile. | ||
// | ||
// This feature is not intended for use by applications, and is thus not documented | ||
// publicly. | ||
var Constructor = function() {}; | ||
Constructor.prototype = (isArray(expression) ? | ||
expression[expression.length - 1] : expression).prototype; | ||
instance = new Constructor(); | ||
|
||
if (identifier) { | ||
if (!(locals && typeof locals.$scope === 'object')) { | ||
throw minErr('$controller')('noscp', | ||
"Cannot export controller '{0}' as '{1}'! No $scope object provided via `locals`.", | ||
constructor || expression.name, identifier); | ||
if (identifier) { | ||
addIdentifier(locals, identifier, instance, constructor || expression.name); | ||
} | ||
|
||
locals.$scope[identifier] = instance; | ||
return extend(function() { | ||
$injector.invoke(expression, instance, locals, constructor); | ||
return instance; | ||
}, { | ||
instance: instance, | ||
identifier: identifier | ||
}); | ||
} | ||
|
||
instance = $injector.instantiate(expression, locals, constructor); | ||
|
||
if (identifier) { | ||
addIdentifier(locals, identifier, instance, constructor || expression.name); | ||
} | ||
|
||
return instance; | ||
}; | ||
|
||
function addIdentifier(locals, identifier, instance, name) { | ||
if (!(locals && isObject(locals.$scope))) { | ||
throw minErr('$controller')('noscp', | ||
"Cannot export controller '{0}' as '{1}'! No $scope object provided via `locals`.", | ||
name, identifier); | ||
} | ||
|
||
locals.$scope[identifier] = instance; | ||
} | ||
}]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
having a separate object for this is suspect... I think elementControllers could probably be used