我从 AngularJS 开始,我想展示这段访问路由参数属性的代码。这样做是否安全/最佳实践?
angular.module('messagecat', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/messages', {templateUrl: '/bundles/acmestore/js/partials/messages-list.html', controller: MessagesListCtrl}).
when('/messages/:messageId', {templateUrl: '/bundles/acmestore/js/partials/message-detail.html', controller: MessageDetailCtrl}).
otherwise({redirectTo: '/messages'});
}]);
/*controller*/
function MessagesListCtrl($scope, $http) {
$http.get('/messages').success(function(data) {
$scope.messages = data;
});
}
/* Does this following bit seem reasonable to you?*/
function MessageDetailCtrl($scope, $routeParams, $http) {
$http.get('/messages').success(function(data) {
$scope.messages = data[$routeParams.messageId];
//console.log($scope.messages);
});
}
the json model:
[
{
"id": 1,
"iam": 1,
"youare": 2,
"lat": 50.8275853,
"lng": 4.3809764,
"msgbody": "Lorem ipsum lorem ipsum lorem ipsum"
}
]