Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I am trying to call a controller from the view. I used Chrome dev tool and I know that the controller is being called, but not execute.

My goal is to call the server to get more information about a channel that I would display after.

I don't really where to look at to make it work. Any advices about how to debug that kind of code?

Controller:

angular.module('guideController').controller('scheduleController', ['$scope', 'API', function($scope, API) {

  $scope.getChannelSchedule = function(channelId) {
    API.getScheduleForChannelId(channelId)
      .success(function(schedule) {
        $scope.schedule = schedule;
      })
      .error(function(error) {
        //ERROR management
      });
  };

}]);

Factory:

angular.module('guideService', [])
  .factory('API', ['$http', function($http) {
    return {
      getScheduleForChannelId: function(channelId) {
        return $http.get('http://localhost:5000/schedule/' + channelId);
      }
    };
  }]);

and the view:

<div class="container" ng-controller="scheduleController">
test {{scheduleController.getChannelSchedule(1234)}}
</div>

Edit: I've been able to solve my problem.

I changed the syntax to

     <tr class="channel-body" ng-controller="channelController" ng-repeat="channel in channels | limitTo:40">
        <td ng-repeat="program in programs">
          {{program.title}}
        </td>
      </tr>

and modified the controller to:

angular.module('guideController')
  .controller('channelController', ['$scope', 'API', function($scope, API) {

    $scope.getPrograms = function() {
      alert($scope.schedule);
      API.getScheduleForChannelId($scope.channel.channelId).
      then(function(data) {
        $scope.programs = data.data;
      });
    };

    $scope.getPrograms();
  }]);

and it worked. I didn't understand how to use the controller. It may be not perfectly designed, but it is working as intended.

share|improve this question

closed as off-topic by PSL, Claies, Sulthan, matthias_h, gsamaras Feb 3 '15 at 1:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – PSL, Claies, Sulthan, matthias_h, gsamaras
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
You need to invoke it right? scheduleController.getChannelSchedule() and it does not make sense to execute a function in interpolation(which returns nothing) and which sets scope property asyncronously. Be careful interpolations gets run every digest cycle. – PSL Feb 3 '15 at 0:30
    
Pretty common in JavaScript, you are assigning your expression to the function itself, rather than the results of the function. you are missing (). – Claies Feb 3 '15 at 0:31
    
Updated my code. While debugging, I tried some funky stuff and what I copy paste was wrong, my bad. I am trying to call a method requiring one parameter, is that legit in angularjs? – plog17 Feb 3 '15 at 0:39
2  
Why are you calling it on the controller when you are adding it to the $scope? – Sulthan Feb 3 '15 at 0:54
    
I tought it was the only way to get access to the data from the server. Should my controller function return the value and how should I code it to do so? – plog17 Feb 3 '15 at 1:10

use

{{getChannelSchedule()}} 

rather than

{{scheduleController.getChannelSchedule}}

If this doesn't work, it may be the way your dependencies are being called

share|improve this answer
1  
You're absolutely right. But I am trying to call a function with a parameter. (code updated) Do this would be the right call {{scheduleController.getChannelSchedule(123)}} ? – plog17 Feb 3 '15 at 0:38
    
yeah that should be fine... is it not working? – Yang Li Feb 3 '15 at 0:42
    
Controller function is not executed. I put a breakpoint in it and it is never reached. – plog17 Feb 3 '15 at 0:46
    
put an alert('hello') at the top of the function and see if it's being called – Yang Li Feb 3 '15 at 0:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.