I am compiling and linking a directive with an isolate scope like so (please note this is manual compiling and linking for reasons outside the scope of this question):
outerElement = angular.element(domElement);
$injector = outerElement.injector();
$compile = $injector.get('$compile');
getTheIsolateScopeForMyDirectiveInstance().myProperty = 'foo'; // Pseudocode. I want myProperty to be available on the scope from inside the controller constructor function.
link = $compile(angular.element('<my-directive></my-directive>'));
// IIUC, the following line will instantiate
// the controller for the directive, injecting
// the isolate scope. I want to augment the
// isolate scope that is injected *before* it
// is injected.
// The value to augment the scope resides in
// *this* execution context.
// How can I do so?
renderedElement = link(outerElement.scope());
element.append(renderedElement);
MyDirective
has an isolate scope (the one I want to augment), and a controller associated with it.
The controller MyDirectiveController
leverages the injector to have its isolate scope injected.
MyDirectiveController.$inject = [ '$scope' ];
I want to augment the isolate scope before it is injected into the instance of MyDirectiveController
, with a value that is only known at run-time in the execution context of the code above.
How can I do this?
MyDirective
function MyDirective() {
return {
scope: {}, // I want to augment this before it is injected into MyDirectiveController
restrict: 'E',
template: template,
controller: 'myDirectiveController',
controllerAs: 'ctrl',
replace: true,
};
}
MyDirectiveController
function MyDirectiveController($scope) {
console.log($scope.myProperty); // Should be 'foo'.
}
MyDirectiveController.$inject = ['$scope'];
If this is impossible, is there another way instance-specific information can be made available to the controller and/or isolate scope of the directive?
The only way I can think of right now is to augment the scope supplied to link(outerElement.scope())
above, and then define a =
property on the isolate scope of the directive.
Edit:
This is what I am now doing, and the myProperty
value ends up on the parent of the isolate scope for the controller:
var isolate = outerElement.scope().$new(true);
isolate.myProperty = 'foo';
renderedElement = link(isolate);
element.append(renderedElement);
Given this, when MyDirectiveController
is instantiated:
function MyDirectiveController($scope) {
$scope.myProperty; // undefined
$scope.$parent.myProperty; // 'foo'
}