Send value as argument to dynamically assigned controller

I am trying to reproduce the modular view of AngularUI and the dialog view angular material the behavior of the dynamic assignment of the controller and the ability to send values ​​as arguments

I tried to simulate AngularUI code without success:

var elem = document.getElementById('placeholder'); elem.setAttribute('ng-controller', 'ctrl'); var injector = angular.element(elem).injector(); var compile = injector.get('$compile'); var rootScope = injector.get('$rootScope'); var controller = injector.get('$controller'); var ctrlInstance = controller('ctrl', {$scope:rootScope, value:'hello'}); var result = compile(elem)(rootScope); 

It works to dynamically assign a controller without passing a value. What am I doing wrong?

http://codepen.io/anon/pen/oXKvyb

+4
source share
1 answer

Do not install the controller on an element .: http://codepen.io/anon/pen/MwNgZx?editors=101

Updated Method:

 function setCtrl() { var elem = document.getElementById('placeholder'); //elem.setAttribute('ng-controller', 'ctrl'); var eSpan = elem.children[0]; var eSelect = elem.children[1]; eSpan.setAttribute('ng-bind', 'name'); eSelect.setAttribute('ng-options', 'o as o for o in rows'); eSelect.setAttribute('ng-model', 'selectedValue'); var injector = angular.element(elem).injector(); var compile = injector.get('$compile'); var rootScope = injector.get('$rootScope'); var controller = injector.get('$controller'); var result = compile(elem)(rootScope); var ctrlInstance = controller('ctrl', {$scope:rootScope, value:'hello'}); } 

Also, when you made your choice, you made children [1] .children [0], and that was wrong.

I assume that if you install ng-controller in an element, angular also tries to instantiate, but does not know where to get the value parameter.

Edit

And I made an example of doing what you wanted him to do .. I think :)

+1
source

All Articles