1

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.

1 Answer 1

3

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

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

1 Comment

@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...

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.