Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular controller not being called

I am new to Angular.js and trying to get get routing working but in my app the second bellow route does not call the listController so I am not getting back data.

I have been through many working examples but cannot seem to work out why this is not working in my app? I get the template back /Categories/SixGrid but as mentioned the controller seem to not being called. Is there something obvious I am not doing wrong? Or can anyone share their knowledge on why this may not be working?

Updated code:

angular.module('App', ['App.Controller']).
    config(['$routeProvider', function ($routeProvider) {
        $routeProvider.when('/', { templateUrl: '/Home/Splash' });
        $routeProvider.when('/Categories/List/:slug', { templateUrl: '/Categories/SixGrid', controller:'listController' });
    }]);

angular.module('App.Controller', [])
    .controller("listController", function ($scope, $http) {
        $http.get('../../api/cat/' + $routeParams.slug).success(function (data) {
                    $scope.products = data;
                });
});
like image 543
ojhawkins Avatar asked Apr 02 '13 12:04

ojhawkins


1 Answers

Try to wrap controller name in quotes:

$routeProvider.when('/Categories/List/:slug', {
    templateUrl: '/Categories/SixGrid',
    controller: 'listController'
});

Also good practice is to define controllers by this way (because global scope remains clean):

angular.module('App').controller("listController", function () {
    //controllerCode
});
like image 94
ardentum-c Avatar answered Sep 23 '22 13:09

ardentum-c