Note that there are some explanatory texts on larger screens.

plurals
  1. POInstantiate and initialize controller in AngularJS
    primarykey
    data
    text
    <p>I have a problem instanciating controller with Angular. I have a main controller <strong>AlkeTypeDefListController</strong> from which I want to dynamically create/remove controllers of type <strong>AlkeTypeDefController</strong>, so I have done that : </p> <p>Code of <strong>AlkeTypeDefListController</strong>:</p> <pre><code>// Create main controller Alke.controller('AlkeTypeDefListController', ['$scope', '$controller', function($scope, $controller) { var primitives = [ ]; // Add some properties to the scope angular.extend($scope, { typedefs : primitives, addTypeDef : function() { var controller = $controller("AlkeTypeDefController", {$scope:$scope.$new()}); $scope.typedefs.push(controller); } }); }]); </code></pre> <p>Code of <strong>AlkeTypeDefController</strong>:</p> <pre><code>// Create main controller Alke.controller('AlkeTypeDefController', ['$scope', '$controller', function($scope, $controller) { // Add some properties to the scope angular.extend($scope, { name : "New Type", fields : [], addField : function() { } }); }]); </code></pre> <p>The html code is this one:</p> <pre><code>&lt;div id="typedefs-editor" ng:controller="AlkeTypeDefListController"&gt; &lt;button ng:click="addTypeDef()"&gt;Add&lt;/button&gt; &lt;button&gt;Remove&lt;/button&gt; &lt;div id="typedef-list"&gt; &lt;ul class="list"&gt; &lt;li ng:repeat="typedef in typedefs"&gt;&lt;a href="#"&gt;{{typedef.name}}&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>The problem does not really come from the instantiation (which works fine), but from the initialization. In fact, when the new "li" appears when I push the "Add" button, the text "New type" (initialized in the controller) does not appear.</p> <p>I think it is about the scope or something like that, but I can't really find how to fix this.</p> <p>I wanted to know if this method seems correct, and also how could I fix the problem I have.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload