3

I tried to make the 'getting started' tutorial from Angular JS, but I have a problem when setting the route of a link: the controller of that route does not get called when the user clicks on the link.

Here is my code:

angular.module('phonecat', []).
config(['$routeProvider',
    function ($routeProvider) {
        $routeProvider.
        when('/phones', {
            templateUrl: 'partials/phone-list.html',
            controller: PhoneListCtrl
        }).
        when('/phones/:phoneId', {
            templateUrl: 'partials/phone-detail.html',
            controller: PhoneDetailCtrl
        }).
        otherwise({
            redirectTo: '/phones'
        });
    }
]);

function PhoneDetailCtrl($scope, $routeParams, $http) {
    $scope.phoneId = $routeParams.phoneId;

    $scope.total = 4;
    $http.get('phones/' + $routeParams.phoneId + '.json').success(function (data) {
        $scope.phone = data;
    });
}

function PhoneListCtrl($scope, $http) {
    $http.get('phones/phones.json').success(function (data) {
        $scope.phones = data;
    });

    $scope.orderProp = 'age';
}
4
  • add quotes around your controllers names when setting routes Commented Oct 26, 2012 at 3:08
  • +1 Exact same issue. Routes and pages are all set up appropriately, clicking the link updates the URL in the browser, but no page are actually loaded. Commented Jan 28, 2013 at 16:34
  • @jaime i think quote around controller name are not required, in one of my demo example i have put up the quotes but it was not working and when i removed the code it started workin. thanks. Commented Aug 13, 2014 at 10:39
  • @PrerakK you are correct. Commented Aug 13, 2014 at 15:16

3 Answers 3

2

What angularjs version did you use? I follow the tutorial too, and it use angular-1.0.0rc7.js and if I look up in the app.js file, it use template rather than templateUrl:

$routeProvider.when('/phones', {
    template: 'partials/phone-list.html',
    controller: PhoneListCtrl
});

so by looking your code, you might want to use angular-1.0.3.js or prior version above the RC

Sign up to request clarification or add additional context in comments.

Comments

1

This should work but you need to be sure the link is inside the DOM element which is the root of your app; ie (ng-app='my-app'). It's hard to tell without seeing the page markup.

2 Comments

I had a similar problem and adding the link worked. But if I go directly to the path or hit the refresh button I get a 404. Any ideas?
Do you have html5mode enabled?
0

The problem was solved by the asker:

I fixed the problem - actually I had the HTML of the next step of this tutorial and it was messing up with the app, mainly because it had binding with attribute that did not exist in the model yet.

1 Comment

Asker added solution as edit to the question; I moved it here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.