Mohit Prajapati
Mohit Prajapati

Reputation: 66

How to pass dynamic value in scope index angular js

I have a ng-model like that:

<tr ng-repeat="user in users">

<input type="text" ng-model="code[user.id]"/>

When I am using $scope.code = {0: 'value'}; then it successfully assigned but when I want to pass dynamic value like:

var index = 0;
$scope.code = {index: 'value'} 

then it will not work.

So my Question is, how to pass dynamic value inside the {}.

Upvotes: 0

Views: 153

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41407

Assign like this

var index = 0; 
$scope.code = {};
$scope.code[index] = 'value'; 

Demo

angular.module("app",[])
.controller("ctrl",function($scope){
var index = 0; 
$scope.code = {};
$scope.code[index] = 'value';
console.log($scope.code);


})

 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 
</div>

Upvotes: 1

Related Questions