Angularjs Prevent the entry of data-free urls

Asked

Viewed 42 times

0

I’m having a problem, I want to make a page that takes values I determined in a video.js file to be returned to a specific url.

.state('video.id', {
url: '/:id',
templateUrl: 'tpl/video_id.html',
})

Being the parameter :id the value I have to type in the browser to pull the specific data from the video.js file and return to the site.

This is being done. However, it also tries to return values without even it exists, can open the page, but there is no record, and can even be opened without any registration.

examples:

site.com/video/ - opens but shows nothing

site.com/video/LALALALA - opens but shows nothing, because there is no such id in the file, if there appeared.

How can I block the page entry when there are no id values in the file?

  • When searching for the id, if it doesn’t exist, you can redirect to another page, video-404.html, for example

  • How to do that? I really don’t think about anything

1 answer

1


you need to check

  1. If the id exists
  2. If the ID is valid

To then redirect if either case is false.

Taking into account that you are using the Ui-router: It would be something like that:

init();

function init(){
  var isValidId = funcaoParaValidarId($stateParams.id);

  if(!$stateParams.id || !isValidId){
    $state.go(404);
  }

}

Reference:

$state go.()

$stateParams

  • Thanks for the references! I got by your method

Browser other questions tagged

You are not signed in. Login or sign up in order to post.