javascript - How can I augment an isolate directive scope before a directive-controller is instantiated? -


i compiling , linking directive isolate scope (please note manual compiling , linking reasons outside scope of question):

    outerelement = angular.element(domelement);     $injector = outerelement.injector();     $compile = $injector.get('$compile');      gettheisolatescopeformydirectiveinstance().myproperty = 'foo'; // pseudocode. want myproperty available on scope inside controller constructor function.     link = $compile(angular.element('<my-directive></my-directive>'));     // iiuc, following line instantiate      // controller directive, injecting      // isolate scope. want augment      // isolate scope injected *before*      // injected.      // value augment scope resides in      // *this* execution context.     // how can so?     renderedelement = link(outerelement.scope());     element.append(renderedelement); 

mydirective has isolate scope (the 1 want augment), , controller associated it.

the controller mydirectivecontroller leverages injector have isolate scope injected.

mydirectivecontroller.$inject = [ '$scope' ]; 

i want augment isolate scope before injected instance of mydirectivecontroller, value known @ run-time in execution context of code above.

how can this?

mydirective

function mydirective() {     return {         scope: {}, // want augment before injected  mydirectivecontroller         restrict: 'e',         template: template,         controller: 'mydirectivecontroller',         controlleras: 'ctrl',         replace: true,     }; } 

mydirectivecontroller

function mydirectivecontroller($scope) {     console.log($scope.myproperty); // should 'foo'. }  mydirectivecontroller.$inject = ['$scope']; 

if impossible, there way instance-specific information can made available controller and/or isolate scope of directive?

the way can think of right augment scope supplied link(outerelement.scope()) above, , define = property on isolate scope of directive.

edit:

this doing, , myproperty value ends on parent of isolate scope controller:

var isolate = outerelement.scope().$new(true); isolate.myproperty = 'foo'; renderedelement = link(isolate); element.append(renderedelement); 

given this, when mydirectivecontroller instantiated:

function mydirectivecontroller($scope) {   $scope.myproperty; // undefined   $scope.$parent.myproperty; // 'foo' } 

the assumption here

the controller mydirectivecontroller leverages injector have isolate scope injected.

mydirectivecontroller.$inject = [ '$scope' ];

i want augment isolate scope before injected instance of mydirectivecontroller, value known @ run-time in execution context of code above.

is wrong. $inject nothing annotation, make difference when code minified.

and can't 'inject' isolate scope before directive linked. ugly monkey-patching trick:

  var _new = scope.$new;   scope.$new = function () {     return angular.extend(_new.apply(this, arguments), {       myproperty: 'foo'     })   };   $compile(angular.element('<my-directive></my-directive>'))(scope);   scope.$new = _new; 

but if possible, appropriate writing tests not production code.

the straightforward way here (and reason why isolated scope used) is

function mydirective() {     return {         scope: {             myproperty: '='         },         ...     }; } 

and

$compile(angular.element('<my-directive my-property="myproperty"></my-directive>'))(scope); 

where scope.myproperty equals 'foo'.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -