D
D
ddidi2015-08-07 12:16:20
Angular
ddidi, 2015-08-07 12:16:20

Angular dynamically change controller by name?

How to do the same with just changing the ng-include
controller ?
only works with 1st binding, not on change

.directive('dynamicCtrl', ['$compile', '$parse', function ($compile, $parse) {
        return {
            restrict: 'A',
            terminal: true,
            priority: 100000,
            link: function (scope, elem) {
                var name = $parse(elem.attr('dynamic-ctrl'))(scope);
                elem.removeAttr('dynamic-ctrl');
                elem.attr('ng-controller', name);
                $compile(elem)(scope);
            }
        };
    }]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-08-07
@ddidi

change the template simply, and "dynamically replace the controller" is an awl. Override the entire directive that the controller refers to.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question