Ron Thomson Ron Thomson - 23 days ago 10
AngularJS Question

How to check if $routeParams data exists

I'm trying to see if there is a way to redirect or perform any action based upon data existing at firebase location using routeParams in angularfire. If I have a user profile $routeParam setup, and the user object exists for user1,(users/user1), I would like to display that profile. However, if the user object doesn't exist for user20, instead of showing the template page with blank data, I would like to redirect to another page. I'm not sure where to make this call.
Here is my Profile Controller setup

app.controller('ProfileCtrl',
function ($scope, $routeParams, User) {
$scope.user = User.findUsername($routeParams.username);


and my User Service for checking if the user exists

findUsername: function (username) {
if (username) {
return users.$child(username);
}

Answer

How about:

app.controller('ProfileCtrl',
    function ($scope, $routeParams, User,$location) {
      if(!$routeParams.username){
         $location.path('/somewhereElse');
      }

    $scope.user = User.findUsername($routeParams.username);

If you mean if you want to check if the username is a valid one, the only way to do so is through some check, ideally server side with $http. I would suggest making a service to check the server for valid usernames.

Comments