admin管理员组

文章数量:1276768

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>

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>
Share Improve this question edited May 15, 2016 at 10:56 Mosh Feu 29.3k18 gold badges93 silver badges141 bronze badges asked Dec 26, 2015 at 23:37 JenuRudanJenuRudan 5451 gold badge14 silver badges30 bronze badges 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? – Ignacio Chiazzo 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) – JenuRudan Commented Dec 26, 2015 at 23:42
Add a ment  | 

2 Answers 2

Reset to default 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;
}

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.

本文标签: javascriptAngularJS ngclick with datatoggleStack Overflow