2016-12-23 11 views
2

すべてのユーザーに個別のページが必要な角度のアプリを作成しています。以下角度jsのURLにidを追加する

localhost/{username} 

app.config(function($routeProvider) { 
$routeProvider 

    .when('/', { 
    templateUrl : '../../views/index_old.html', 


    }) 

    .when('/email/signup', { 
    templateUrl : '../../views/emailsignup.html', 
    controller : 'myCont' 

    }) 

    .when('/email/code/', { 
    templateUrl : '../../views/emailcode.html', 
    controller : 'codeCheck' 

    }) 

    .when('/user/basic/', { 
    templateUrl : '../../views/basicInfo.html', 
    controller : 'userbasicInfo' 

    }) 

    .when('/user/location/', { 
    templateUrl : '../../views/userLocation.html', 
    controller : 'userLocation' 

    }) 

});  

私はURLでユーザー名を渡すことができませんよ、私のルーティングのparamです。 これを行う方法はありますか?

+0

あなたはパラメータとしてユーザー名を渡すことを意味しますか? –

+0

はい、about.meサイトのようなものが必要です。 about.me/ashishverma〜ここで、ashishverma〜はユーザー名です –

答えて

1

次のようなもののルートを作成することができます。

app.config(function($routeProvider) { 
    $routeProvider 

    .when('/:username', { 
    templateUrl : '../../views/index_old.html' 

    }).when('/userprofile/:profileID', { 
    templateUrl : '../../views/index_old.html' 

    }) 
}) 
0

あなたはこのような$routeProvider気にいらない使用する必要があります。このルートは、URLにパラメータとしてuserIdが含まれています

.when('aboutMe/:userId', {   
     templateUrl: "app/aboutMe/aboutMe.html", 
     controller: "exerciseEditCtrl", 
     resolve: 
      selectedUser:['UserResourceFactory','$routeParams',function(exerciseResourceFactory, $stateParams){ 
       return UserResourceFactory.get({id: $routeParams.userId}); 
      }] 
     } 
    }); 

を。だからresolveを使って、UserResourceFactoryというサービスを使っているユーザーの情報を取得しました。これにより、コントローラを起動する前にユーザー情報を確実に取得できます。お使いのコントローラでは、このようなparametarとしてselcetedUserを持っています:

app.controller('AboutMeCtrl, ['$scope', 'selectedUser',function($scope, selectctedUser){//your controller to user selectedUserData } ]); 

私に知らせてください明確ではない何か場合。乾杯。

0

あなたはこのコントローラcodeCheckuserbasicInfoでユーザーオブジェクトの処理ログインページ

angular.module('').controller('codeCheck',function($scope,$location,appVariables){ 
     //your code for the controller 

     $scope.userId= appVariables.user; 

     login=function(){ 
     $location.path('/user/basic/'+ userId); 
     } 

}); 

angular.module('yourModule').factory('appVariables',function(){ 
    //Your application variables 
    var userId=0; 
    return{ 
    userId:userId, 
} 
}); 

に対応すると仮定すると

app.config(function($routeProvider) { 
$routeProvider 

    .when('/', { 
    templateUrl : '../../views/index_old.html', 
    }) 
.when('/email/signup', { 
    templateUrl : '../../views/emailsignup.html', 
    controller : 'myCont' 
}) 
.when('/email/code/', { 
    templateUrl : '../../views/emailcode.html', 
    controller : 'codeCheck' 
}) 
.when('/user/basic/userId', { 
    templateUrl : '../../views/basicInfo.html', 
    controller : 'userbasicInfo' 
    params:{ 
     userId:null 
    } 
    }) 
    .when('/user/location/', { 
    templateUrl : '../../views/userLocation.html', 
    controller : 'userLocation' 
    }) 
}); 

以下のように設定ファイルにルートのparamsを追加することができますコントローラ

angular.module('').controller('userbasicInfo',function($scope,$http,appVariables){ 
     //your code for the controller 

     //API call using $http to get the corresponding user details with the help of the user Id 

}); 
関連する問題