Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

modalLoginController.js

    angular.module('App')
  .controller('modalLoginController', function ($modal, $scope, loginService) {

  $scope.userName = null;
  $scope.userPassword = null;

  $scope.open = function (size) {

    var modalInstance = $modal.open({
      templateUrl: 'modalLogin.html',
      controller: ModalInstanceCtrl,
      size: size,
      resolve: {
        loginDto: function() {
          var login = {};
          login.userName = $scope.userName;
          login.userPassword = $scope.userPassword;

          return login;
        }
      }
    });
  };

  var ModalInstanceCtrl = function ($scope, $modalInstance, $window, loginDto) {

    $scope.ok = function () {
      var login = {};
      login.userName = $scope.userName;
      login.userPassword = $scope.userPassword;

      $window.alert(login.userName + ' ' + login.userPassword);

      var response = loginService.validateLogin(loginDto);
      if (response.success) {
        $modalInstance.close();
      } else {
        $window.alert('zebrero');
      }
    };

    $scope.cancel = function () {
      $modalInstance.dismiss('cancel');
    };

  };

});

Index.html

    ...
...
...
<script type="text/ng-template" id="modalLogin.html">
    <div class="modal-header">
        <h3 class="modal-title">Login</h3>
    </div>
    <div class="modal-body">
        <input type="text" ng-model="userName" placeholder="user"/>
        <input type="password" ng-model="userPassword" placeholder="password"/>
    </div>
    <div class="modal-footer">
        <button class="btn btn-default" ng-click="ok()">Login</button>
        <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
    </div>
    <div class="modal-body" ng-show="isLogged">
        <label>Usuário Logado</label>
    </div>
</script>

</head>
<body ng-controller="MainCtrl">  
...
...
...
    <div id="loginLogout" ng-controller="modalLoginController">
        <button  class="btn btn-default" ng-click="open('sm')" modalLogin ng-show="!isLogged" >Login</button>
    </div>
...
...
...

Why I can't get the $scope.userName and $scope.userPassword defined in ModalInstanceCtrl, even though I can use $scope.ok and $scope.cancel? Just for text I put $scope.userName and $scope.userPassword in resolve as loginDto, but doesn't work too.

share|improve this question

1 Answer 1

The modal controller has it's own scope. You haven't defined $scope.username within it.

You need to access these from the loginDto object that you injected.

Try:

$scope.userName = loginDto.userName;
share|improve this answer
    
Doesnt work... I tried –  user2657065 Jul 22 at 11:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.