Attempting to replicate the while loop behavior of pure Javascript in AngularJS. The goal is to invoke the createPlant() method a certain number of times based on the quantity input value when the "add" button is clicked. This code snippet achieves that by adding to an array 'plants':
$("#add").on("click", function() {
var i = 0;
while (i < $("#pQuantity").val()) {
createPlant();
i += 1;
};
Here's the Angular implementation:
HTML
<button ng-click="addPlant()" type="button">Add</button>
Despite uncertainty, here's the incorrect attempt at the script:
JS
var i = 0;
while (i < $("#pQuantity").val()) {
$scope.addPlant = function() {
var plant = {name: $("#pVariety").val()};
$scope.plants.unshift(plant);
};
i += 1;
};