0

I have a form where I'm taking a input and according to that creating a number of input boxes. The javascript code is given below for that.

for(i = 1; i <= c; i++) { //c = total input count
    block = block + '<div class="row">'+
            '<div class="form-group col-lg-12">'+
                '<input id="name" ng-model="new.var'+i+'name" type="text" class="form-control">'+
                '<input id="type" ng-model="new.var'+i+'type" type="text" class="form-control">'+
            '</div>'+
            '</div>';
}
document.getElementById("vars").innerHTML = block;

Above code is working fine & ng-model are getting generated dynamically fine, like new.var1name , new.var1type , new.var2name , new.var2type and so on.

But how to get those variables in my controller? If I generate those variables in my controller like below then its giving error that it cant find 'name'.

var var1 = [];
for(i = 1; i <= c; i++) { //c = total input count
    var item = {};
    console.log('var'+i+'name', 'var'+i+'unit');
    item['name'] = $scope.new.var+i+name;
    item['type'] = $scope.new.var+i+type;
    var1.push(item);
} 
console.log(JSON.stringify(var1));

so I have used like below but now no error is there but the var1 is empty.

var var1 = [];
for(i = 1; i <= c; i++) {
    var item = {};
    console.log('var'+i+'name', 'var'+i+'type');
    item['name'] = $scope.new['var'+i+'name'];
    item['type'] = $scope.new['var'+i+'type'];
    var1.push(item);
}
console.log(JSON.stringify(var1));

Please anyone help me to find what I'm doing wrong or is it possible to do???

2
  • 1
    Note that the $scope . new key is reserved for angular. This is a function that is used to create a copy of scope Commented Jun 5, 2015 at 5:20
  • @wZVanG but all forms are working well. Only this dynamic one not working Commented Jun 5, 2015 at 7:50

2 Answers 2

1

I would suggest that, rather than generate the html in your controller, you write this html in your view, and repeat it using angular's ng-repeat directive: https://docs.angularjs.org/api/ng/directive/ngRepeat. Something like this (not tested):

<div ng-repeat="item in c track by $index">
    <input id="name" ng-model="name[$index]" type="text" class="form-control">
    <input id="type" ng-model="type[$index]" type="text" class="form-control">
</div>

You should then be able to access the name and type arrays from $scope within your controller. If 'c' is the ng-model for your original input (which defines the number of times to repeat) then new fields should get added dynamically if this the desired functionality?

EDITED

in your controller

$scope.c = 5;
$scope.getNumber = function(num) {
  return new Array(num);   
}

in your view

<div ng-repeat="i in getNumber(c) track by $index">
    <input id="name" ng-model="name[$index]" type="text" class="form-control">
    <input id="type" ng-model="type[$index]" type="text" class="form-control">
</div>
Sign up to request clarification or add additional context in comments.

3 Comments

But c is the count. Means it is an integer will contain integer value only. Then how will it repeat?? And the javascript which is adding input boxes is currently in my view.
Apologies, yes you are correct - you can only repeat on a collection such as an array or object. I've updated my original answer. Thanks :-)
Re: javascript in the view - its usually desirable to separate the two, so that the view contains html markup and the js goes in the controller
0

use $scope['new.var'+i+'name']

there are two ways accessing object property in JavaScript:

  1. if you know the name, you can do $scope.new.var1name
  2. if the name is dynamic, which is your case, you can use the bracket syntax $scope['new.var'+i+'name']

2 Comments

could you explain it how will it work..with an example
@pankajparkar udpated, please check

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.