Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How can i pass a dynamic variable to selection for my ng-model of checkbox. Here is the the jsfiddle link.

http://jsfiddle.net/bmleite/PQvQ2/

Here i want to replace the id in .js file with a variable as:

function Ctrl($scope) {

    $scope.categories = [ { "name": "Sport", "id": "50d5ad" } , {"name": "General", "id": "678ffr" } ];

    var someId = $scope.categories[1].id;
    $scope.selection = {
        ids: {someId: true}
    };
}

But it is not working. If i replace "someId" with "General", it works fine.

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted

you can use object properties in two ways

object.property1 or object[property1]

both will produce same result, so if you chage your js file to this,

var app = angular.module('app', []);

function Ctrl($scope) {

    $scope.categories = [{
        "name": "Sport",
        "id": "50d5ad"
    }, {
        "name": "General",
        "id": "678ffr"
    }];

    //define selection object
    $scope.selection = {ids : {}};

    angular.forEach($scope.categories, function(category){
        $scope.selection.ids[category.id] = true;
    });

}

it will give you to right result here is update JSFIDDLE...

share|improve this answer
    
Thanks wickY :) –  tanuj Mar 24 at 9:59
    
@tanuj you are welcome... please accept it as answer and upvote if it solves your problem, otherwise tell me and I try to help you further... –  wickY26 Mar 24 at 11:36
    
sorry man it requires 15+ reputation to vote up :( –  tanuj Mar 25 at 7:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.