1

I'm trying to bind id in my controller

 obj1["id"] = $scope.editableCourse.Modules[i].id; obj["id"] = $scope.editableCourse.Modules[i].Resourses[j].id; 

but in run time, got an

error: "Cannot set property 'id' of undefined".

How can I initialize "id" in my controller for the above cases.

Anyone suggest me a solution.

5
  • Either the modules or the resources is undefined. You need to find out why. Commented Oct 21, 2016 at 8:52
  • The id is not the problem, the undefined object or controller is the problem. Commented Oct 21, 2016 at 8:54
  • You need to initialise the varibales $scope.editableCourse,obj1,obj etc. Commented Oct 21, 2016 at 8:58
  • Your obj1 or obj is undefined therefor you can't set property on it. Depending on your case you could do var obj = {} to fix this Commented Oct 21, 2016 at 8:59
  • my question is how to declare $scope.editableCourse.Modules[i].id in controller rest of the all declarations are perfect. Commented Oct 21, 2016 at 10:44

1 Answer 1

1

You should use var obj={}; and var obj1={};

Whenever an object is declared its default value will be null like var obj or simply obj will be considered as var obj=null or undefined; and you cannot set any property like id for this undefined object.

so your code should be

 var obj = {}; var obj1 = {}; obj1["id"] = $scope.editableCourse.Modules[i].id; obj["id"] = $scope.editableCourse.Modules[i].Resourses[j].id; 
Sign up to request clarification or add additional context in comments.

2 Comments

Please add some explanation of why this code helps the OP. This will help provide an answer future viewers can learn from. See How to Answer for more information.
Give me a suggestion how to declare $scope.editableCourse.Modules[i].id this reference variable

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.