Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: create element dynamically

How do I go about create an element in my controller? e.g. on a click event?

example controller:

function AddCtrl($scope){

    $scope.add = function(){
         // do stuff to create a new element?
    }


}

example view:

<div ng-controller="AddCtrl">

    <button ng-click="add()">Add</button>
    // create <input type="text" ng-model="form.anotherField">

</div>

Any suggestions much appreciated.

like image 831
Iladarsda Avatar asked Dec 29 '25 19:12

Iladarsda


1 Answers

AngularJS is intended to follow MVC - so the controller creating an element in the view doesn't agree with the MVC behavior. The controller should not know about the view.

It sounds as if you want to have a control appear based on some conditional logic. One approach would be to bind to the visibility of the element.

like image 186
Mike Weber Avatar answered Dec 31 '25 14:12

Mike Weber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!