Pregunta

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);
        }    
¿Fue útil?

Solución

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.

Otros consejos

if the parameter is optional and not present then $routeParams will be undefined and the above will fail. To avoid this just simply check for $routeParams as well

if($routeParams && routeParams.username) {
    $scope.user = User.findUsername($routeParams.username);
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top