0

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 {}.

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

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.