Links in Angular

Asked

Viewed 423 times

0

I’m having some headaches, because I need to create a function to validate when the link should redirect to the place or when it should not for example if I am loading an image and it click the link should not load, I created a function like this in jquery, but I need one that’s angled, you can help me?

follows the function in jquery:

$(document).ready(function () {
    $('a').click(function (event) {
        var url = $(this).attr('href');
        if (alterado === true) {
            //console.log("teste");

            bootbox.confirm('Existem dados não salvos, deseja continuar?', function (resp) {


                if (resp) {
                    location.href = url; // acessa o link indicado na tag a
                } else {
                    event.preventDefault(); // cancela o evento
                }
            });
            return false;
        }
    });
});
  • this "changed" variable comes from where?

  • of another controler, only a function that returns true or false

  • This code works in jQuery and you want to convert it to Angularjs, this?

  • exact, create a function that does the same thing, but that is in Engineering

1 answer

1


I didn’t quite understand your question, but if you want to do something only when the page(route) is fully loaded you can use the $viewContentLoaded.

Example:

Controller:

  var redirecionar = false;
  $scope.$on('$viewContentLoaded', function() {
      redirecionar = true;
      console.log("Rota carregada")
  });

 $scope.acessar = function() {
     if (redirecionar) {
         console.log("Página Carregada! Redirecionando..")
         //implementar redirecionamento
     } else {
         console.log("Pagina não carregada!")
     }
 }

View:

<a href="" ng-click="acessar()">Clique Aqui!</a>
  • Sorry Tchies, I don’t think I expressed myself well, for example: when I have an input text != '' it will not allow redirect but if the input === '' it will redirect, can understand me?

  • As such an input?

  • is an example when I have an input field screen it will validate if there has been any change in the field and if there is any change in the field contents it will not be able to redirect, ai will have to send a $mdDialog wondering if you want to proceed or not and that will lose the data if you proceed there if you click yes it will allow redirect otherwise no

  • hahah managed to understand now friend?

  • The input value will be default then?

  • will be validated inside an ng-change for a variable called $Scope.foiAltered = true/false

  • That would be about right: http://jsfiddle.net/95wEL/22/

  • face, by what I have checked anyway it will redirect --- otherwise({redirectTo: 'homepage'}); --- and should not redirect in any way even to the same page, understand, like it should cut the effect of href if it does not have false the return, can understand?

  • Face is why you should put how you want to redirect, in question you did not specify where you want to redirect. If the otherwisewill only take effect if you call a route non-existent.

  • Inside the if is just doing the redirect logic

  • Redirect by controller and not by href

Show 6 more comments

Browser other questions tagged

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