Assign controller to element dynamically after bootstrap
Asked Answered
D

2

6

I have an AngularJS app that I manually bootstrap at time 't'. At time 't + 1', I would like to show an HTML element that has no ng-controller attached. I would like to dynamically add a ng-controller to this element so it can communicate with my javascript code.

How can I do that?

PS I tried to dynamically add the ng-controller attribute to the element but it doesn't work.

Downstream answered 19/8, 2015 at 18:26 Comment(2)
you wanted to add controller dynamically or wanted to assign controller to element dynamically..I'm confused..Hellraiser
@PankajParkar Would like to assign controller to element dynamicallyDownstream
C
3

To do that you need to compile elements.

HTML

<!DOCTYPE html>
<html ng-app="app">
<head>
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet" type="text/css" />

    <link href="main.css" rel="stylesheet" type="text/css" />
    <script src="main.js"></script>
<meta charset=utf-8 />
<title>Angular JS</title>
</head>
<body ng-controller="parentCtrl">
    <button ng-click="setCtrl2()">Set Controller</button>
    <div id="placeholder">
        <span></span>
        <select>
            <option></option>
        </select>
        <span class="input-append">
            <input id="add" type="text" placeholder="Another one ?" ng-model="addName" />
            <input type="submit" class="btn btn-primary" ng-click="addRow()" value="+ add" />
        </span>
    </div>
</body>
</html>

Javascript

angular.module('app', []);

angular.module('app').controller('ctrl', function($scope) {
      $scope.rows = ['Paul','John','Lucie'];
        $scope.name = 'Jack';

      $scope.addRow = function(){
        $scope.rows.push($scope.addName);
        $scope.addName = "";
          console.log($scope.rows);
      };
})
.controller('parentCtrl', function($scope) {
    $scope.setCtrl2 = setCtrl;
});

function setCtrl() {
    var elem =  document.getElementById('placeholder');
    elem.setAttribute('ng-controller', 'ctrl');
    var eSpan = elem.children[0];
    var eSelect = elem.children[1].children[0];
    eSpan.setAttribute('ng-bind', 'name');
    eSelect.setAttribute('ng-repeat', 'row in rows');
    eSelect.setAttribute('value', '{{ row }}');
    eSelect.setAttribute('ng-bind', 'row');

    var injector = angular.element(elem).injector();
    var compile = injector.get('$compile');
    var rootScope = injector.get('$rootScope');
    var result = compile(elem)(rootScope);
    // When outside of AngularJS you need to call digest
    // rootScope.$digest();
}

See: http://codepen.io/skarllot/pen/LVKpgd

Checani answered 19/8, 2015 at 20:27 Comment(2)
@Checani Very nice! One more question, how can I send variables to my controller if it requires? Let say my controller is: angular.module('app').controller('ctrl', function($scope, variable1, variable2) {Downstream
@Checani Unfortunately it doesn't work! I need to pass these variables not to initialize them.Downstream
C
1

You don't mention whether you've looked at previously asked questions. I found two which look like they could address the question you've asked:

Loading an AngularJS controller dynamically

How to bind an AngularJS controller to dynamically added HTML?

Cerellia answered 19/8, 2015 at 20:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.