angularjsangular-ui-bootstrap

how to close modal after submit in angularjs


i know have many result in this question but no one give work answer for me.

html 1:

<button class="basebtn logbtn" ng-click="openlog()">LOGIN</button>
<button class="basebtn regbtn" ng-click="openreg()">SIGN UP</button>

html 2:

<div ng-controller="main">
<center><img class="img-login" ng-src="../images/form-logo.png"/></center>
<form class="login" name="log" ng-submit="login()" novalidate>
    <div class="form-group  login-form" ng-class="{'has-error' : isInvalid(log.email) ,'has-success' : isValid(log.email)}">
        <input type="email" class="form-control" name="email" ng-model="user.email" required placeholder="Email">
        <div class="alert alert-danger" role="alert" ng-show="isInvalid(log.email)">Enter valid email</div>
    </div>
    <div class="form-group  login-form" ng-class="{'has-error' : isInvalid(log.password) ,'has-success' : isValid(log.password)}">
        <input type="password" class="form-control" name="password" ng-model="user.password" required placeholder="Password">
        <div class="alert alert-danger" role="alert" ng-show="isInvalid(log.password)">This Feild is required</div>
    </div>
    <div class="form-group" style="text-align:center;">
        <input type="submit" class="logbnt" value="LOGIN" ng-disabled="! log.$valid" data-dismiss='modal'/>
    </div>
</form>

angular :

    $scope.login = function(){
    $http.post("www.example.com/login",$scope.user).then(function(res){
        localStorage.setItem("token",res.data.token);
        //how i close the modal ???
    });
}

//here create the modal
$scope.openlog = function () {
        $scope.$modalInstance = $uibModal.open({
        templateUrl:"login.html",
    });
}

that my code , how i close in $http result the modal ?


Solution

  • You can do this,

     $scope.login = function(){
           $http.post("www.example.com/login",$scope.user).then(function(res){
            localStorage.setItem("token",res.data.token);
            $modalInstance.dismiss('cancel');
          });
        };