Liam
Liam

Reputation: 9855

Update URL with angular on content change

I'm trying to update my URL when loading new content with Angular. I have the following only I'm getting the error $location is not defined?

app.controller('mainController', function($scope) {
   $scope.title = 'Services';
   $scope.message = 'Full-service support';
   $location.path('/user/' + $scope.userId, false);
});

FULL JS

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

    app.config(function($routeProvider) {

      $routeProvider

      .when('/', {
        templateUrl: 'pages/home.html',
        controller: 'mainController'
      })

      .when('/personal', {
        templateUrl: 'pages/personal.html',
        controller: 'personalController'
      })

      .when('/contacts', {
        templateUrl: 'pages/contacts.html',
        controller: 'contactsController'
      });

    });

    app.controller('mainController', function($scope, $location) {
        $scope.title = 'Services.';
        $scope.message = 'Full.';
        $location.path('/user/', false);
    });

    app.controller('personalController', function($scope, $location) {
        $scope.title = 'Services.';
        $scope.message = 'Full.';
        $location.path('/user/', false);
    });

    app.controller('contactsController', function($scope, $location) {
        $scope.title = 'Services.';
        $scope.message = 'Full.';
        $location.path('/user/', false);
    });

Upvotes: 0

Views: 130

Answers (1)

charlietfl
charlietfl

Reputation: 171669

$location is not a global....you need to inject it where you intend to use it

app.controller('mainController', function($scope, $location) {
                                                  //^^ inject $location
   $scope.title = 'Services';
   $scope.message = 'Full-service support';
   $location.path('/user/' + $scope.userId, false);
});

Upvotes: 2

Related Questions