문제

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);
        }    
도움이 되었습니까?

해결책

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.

다른 팁

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top