nam vo
nam vo

Reputation: 3437

AngularJS - simple routing not working

I have a route with parameter like this

 var app = angular.module("myapp", ["ngRoute"]);

    app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
            $locationProvider.hashPrefix("!").html5Mode(true);

            $routeProvider

            .when('/sukien', { templateUrl: '/app/views/sukien/index.html' , controller: 'eventCtrl' })
            .when('/sukien/:id', { templateUrl: function (params) { return '/app/views/sukien/index.html?id=' + params.id }, controller: 'eventCtrl' })


        }])

why /sukien works and /sukien/:id doesn't ? indeed, angularjs seems not to understand what it is. "Uncaught TypeError: undefined is not a function"

/sukien/333 => failed to work.

Upvotes: 0

Views: 61

Answers (1)

AlexHv
AlexHv

Reputation: 1734

You are mixing template url, state of your application and search parameters it seems.

The templateUrl tells angularjs where to look for the html file : it will very unlikely depend on the params.id and be set via a function, but will rather be a constant.

It has nothing to see with the url that the user sees in their browser.

For example : '/app/views/sukien/suiken.html'

The url the user sees will be something like :

.../suiken/1223445

And you can then access the id in your controller via $routeParams.id

Upvotes: 1

Related Questions