5

I have a list item which toggles modal and sets a param using ng-click the problem is when calling a function in any other place which logs Course.SelectedCourse it's undefined although Course.ID has a value.

<li class="facebook" style="width:33%;">
   <a ng-click="Course.SelectedCourse = Course.ID" data-toggle="modal" data-target="#myModal">
      <span class="glyphicon glyphicon-user"></span>
   </a>
</li>
2
  • This take place also after you selected one item of the list, or only the first time when you not yet click a button? Commented Dec 26, 2015 at 23:40
  • i clicks that button which opens a modal that has a button that calls a function "dummyfun(Course.SelectedCourse)" where i log (Course.SelectedCourse) Commented Dec 26, 2015 at 23:42

2 Answers 2

4

Use a function in the controller, this might look like this :

In the view :

<li class="facebook" style="width:33%;" >
  <a ng-click="setSelectedCourse(Course.ID)" data-toggle="modal" data-target="#myModal">
    <span class="glyphicon glyphicon-user"></span>
  </a>
</li>

In the controller

function setSelectedCourse(course_id){
  $scope.Course.SelectedCourse = course_id;
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works for me. Does anyone have explanation for this behaviour ? What is the difference ? Why doesn't this work: ng-click="Course.SelectedCourse = Course.ID" ?
1

In AngularJS, directives have their own scope, your list is generated with ng-repeat, isn't it ? That's why

"Course.SelectedCourse = Course.ID" 

doesn't work, because inside the div ng-repeat is looping $scope.Course is created locally and isn't the same that the one in your main controller. Besides, functions from main controller can be called by directives, That's why the previous answer works.

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.