在URL中隐藏参数值(GUID) [英] Hide param value (guid) in an URL
本文介绍了在URL中隐藏参数值(GUID)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的路线上
.when('/user:user_guid',{
templateUrl : 'users/profile.html',
controller : 'userController'
})
我在ng-repeat
内的index.html
中
<a href="#/user/:{{user.user_guid}}">view profile</a>
它可以工作,但是在我的URL中,我看到了GUID已被暴露.有什么办法藏起来吗?
It works but in my URL I'm seeing the guid has been exposed. Is there any way to hide it?
推荐答案
使用 ng-click
<a ng-click="go_url(user)">view profile</a>
然后在某些控制器中
//it has many possible solutions, this is one of them
//according to your config, you're using angular route provider
$scope.go_url = function(_user){
//be sure to inject $location service
$location.path('/user/' + _user.user_guid);
}
如果您不想在浏览器中显示URL,请使用可以将guid变量保留在其中的服务,并在用户导航到/user
If you don't want to expose URL in browser, use service that can hold guid variable inside, and grab it when user navigates to /user
这是演示
angular.module('demo', ['ngRoute']);
angular.module('demo').config(function($routeProvider){
$routeProvider
.when('/welcome', {
templateUrl: 'welcome_page'
})
.when('/user', {
templateUrl: 'hello_page'
})
.otherwise({redirectTo: '/welcome'});
});
angular.module('demo').service('SecretServ', function(){
this.secret_guid = '';
});
angular.module('demo').controller('Controller1', function($scope, $location, SecretServ){
$scope.user = {
guid: '123345567234'
};
$scope.go = function(){
SecretServ.secret_guid = $scope.user.guid;
$location.path('/user');
};
});
angular.module('demo').controller('Controller2', function($scope, SecretServ, $location){
$scope.guid = SecretServ.secret_guid;
$scope.exit = function(){
$location.path('/welcome');
}
});
<script src="https://code.angularjs.org/1.4.4/angular.js"></script>
<script src="https://code.angularjs.org/1.4.4/angular-route.js"></script>
<div ng-app="demo">
My app
<div ng-view></div>
<script id="welcome_page" type="text/ng-template">
<div ng-controller="Controller1">
Welcome<br>
To go to private page, click <button ng-click="go()">me</button>
</div>
</script>
<script id="hello_page" type="text/ng-template">
<div ng-controller="Controller2">
Hello {{guid}}, <button ng-click="exit()">exit</button>
</div>
</script>
</div>
这篇关于在URL中隐藏参数值(GUID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文