Alert angular confirm

0

Suppose that in my controller I have:

var confirm = true;

and in my html:

    <a href="index" onclick="return confirm('Are you sure?')">Index</a>

How can I do so:

  • if the variable "confirm" is true, leave a message saying, "Surely you want to return?", if you give it back and if it does not, it remains
  • if the variable "confirm" is false directly redirect

NOTE: I would not like to go through the controller to do this check

    
asked by sirdaiz 19.07.2017 в 10:27
source

1 answer

1

I have understood two different things:

1: The <a> depends on the variable confirm .

2: The action depends on the result of confirm .

1: First declare confirm using $scope . Then it determines the appearance of the <a> using the property ng-if . This way you can see that if confirming is false it is not shown, and if it is true if.

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.confirmar = true;
});
<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.2.22/angular.js" data-semver="1.2.22"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
      <a href="index" ng-if='confirmar' onclick="return confirm('Are you sure?')">Index</a>
  </body>

</html>

2:

Treat the true or false by a function and execute the desired result. I have not put the redirects because I do not know what you want to put exactly.

function confirmar(){
var r = confirm("¿seguro que desea volver?");
if (r == true) {
    alert("\'codgio de vuelta (true)\'");
} else {
    alert("\'codgio de redireccion (false)\'");
}
}
<a href="#" onclick="return confirmar();">Index</a>
    
answered by 19.07.2017 в 11:52