jennath.nithisha jennath.nithisha - 6 months ago 9
Javascript Question

Call a Action method from a $scope function()

I have a multi-level repeating accordion. On page load , the data for the first accordion will be bind. When I click on each of the level-1 accordion, the unique ID should be passed to a Action method and the data for the next-level accordion should bind.
I am not able to pass this ID and call the Action method from angular js controller. Can anyone suggest what I am doing wrong here.

CSHTML

<div id="itineraryAccordion" ng-repeat="itinerary in item.Itineraries">
<div>
<a data-toggle="collapse" data-parent="#itineraryAccordion" href="#collapseItinerary" aria-expanded="false" class="collapsed" ng-bind="itinerary.Name" data-ng-click="GetSchedule(itinerary.Name)"></a>
</div>
<div id="collapseItinerary" class="panel-collapse collapse" aria-expanded="false">
<div class="box-body">
<div class="box-group" id="scheduleAccordion" ng-repeat="schedule in scheList">
<div>
<a data-toggle="collapse" data-parent="#scheduleAccordion" href="#collapseSchedule" aria-expanded="false" class="collapsed" ng-bind="schedule.ScheduleName" > </a>
</div>


SCRIPT

myApp.controller('layoutController', function ($scope, $http) {
$scope.GetSchedule = function (itineraryName, $scope, $http) {
alert("Itinerary Id is " + itineraryName);
$http.get("../SchedulePlan/GetSchedule?itineraryName=" + itineraryName).success(function (response) {
$scope.scheList = response;
})
};


Here , I am getting the itineraryName in the Alert(), but in the next line, $http.get() is not working.

SSH SSH
Answer

try this. you don't need to put $scope and $http service in function.

 myApp.controller('layoutController', function ($scope, $http) {
  $scope.GetSchedule = function (itineraryName) {
   alert("Itinerary Id is " + itineraryName);
    $http.get("../SchedulePlan/GetSchedule?itineraryName=" + itineraryName).success(function (response) {
    $scope.scheList = response;
  })
 };