0

Im trying to get values from a $rootScope object which was binded to the checkboxes.

HTML:

<tr ng-repeat="system_history in SystemHistoryResponse.system_history">
<td><input type="checkbox" ng-model="versionValues[system_history.version]"></td>
</tr>

JS:

$rootScope.versionValues = {};

Output now:

versionValues: {"321":true,"322":true}

Desired output:

versionValues: [321, 322]

1 Answer 1

1

I would just convert the object into an array when I needed it:

var a = {"321":true,"322":true};
var b = [];
var i = 0;
for(x in a){ 
    b[i++] = parseInt(x,10);
}
// b === [321,322]

Otherwise, you could use the ngChange directive(NOTE: I haven't tested this code):

html:
<input type="checkbox" ng-model="versionValues[system_history.version]
ng-change="update(system_history.version)"
">
js:
$rootScope.versionValues = {};
$rootScope.versionValuesArray = [];
$rootScope.update(version){
    if($rootScope.versionValues[version]){
        // add version to $rootScope.versionValuesArray
    }else{
        // remove version from $rootScope.versionValuesArray
    }
}

But the problem with that is that it takes linear time to insert and remove from an array, which is of the same magnitude as the time to convert the entire object into an array.

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

1 Comment

the second method worked just fine!! thank you soo much!

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.