AngularJS ng-click with data switching

I have a list item that switches the modal and sets the parameter using ng-click the problem is the function call in any other place that Course.SelectedCourse it undefined registers, although Course.ID does matter.

 <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> 
+5
source share
2 answers

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

In 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; } 
+4
source

AngularJS directives have their own scope, your list is generated using ng-repeat, right? That's why

 "Course.SelectedCourse = Course.ID" 

doesnโ€™t work because inside the ng-repeat div the $ scope loop is executed. Theourse is created locally and does not match what is in your main controller. In addition, functions from the main controller can be called with directives, so the previous answer works.

0
source

Source: https://habr.com/ru/post/1239278/


All Articles