fyeah
fyeah

Reputation: 59

How to declare a dynamic ng-model? angularjs

I was trying to make dynamic input. is it possible to have dynamic ng-model by index?

See scrnsht here

supposedly barcode ng-model from above should be set into ng-model="barcode_0" and ng-model="barcode_1"

I have tried this code, but it doesnt work

//controller
console.log($scope['Barcode_' + index])
<!-- HTML -->
<input type="text" ng-model="Barcode_[$index]" class="form-control" placeholder="Stock ID" >
Anyone? please help

Upvotes: 0

Views: 509

Answers (1)

Meliodas
Meliodas

Reputation: 150

You are on the right track, you just need a little change in the code.

Barcode_[$index] is an element of the array Barcode_. You just need to initialize this array in the controller, and then you can use$scope.Barcode_[0] and $scope.Barcode_[1] in your controller to access the variables.

Leave the html code as it is, and Do this is your controller:

$scope.Barcode_ = [];//initializing the array

You can access the variables like this in this controller:

console.log($scope.Barcode_[0]);//prints first one
console.log($scope.Barcode_[1]);//prints second one

Upvotes: 1

Related Questions