简体   繁体   中英

Angularjs doesn't update scope when calling a function?

I have a Modal service I am using for my Modals, in the service I pass the scope of the controller so that the modal's view has access to the scope object. When I update the scope using a function the value never gets updated.

I have tried using $apply but the eror I get is that the digest loop is already running.

So here is the code

.controller('LoginCtrl', ($scope, $location, $rootScope, Modal, $log, $http) ->

  $scope.placeholder = {
    email: 'Email Address',
    password: 'password'
  }

  $scope.login = (email, password)->
    $http.post('/dashboard/login', {
      email: email,
      password: password
    }).error((data, status)->
      $scope.invalidPassword = "lalala"
      $scope.$apply()
    ).success (data, status) ->
      $rootScope.userAuth = true
      $rootScope.userInfo = data
      $state.go('dashboard')
      $modalInstance.close()

  Modal.custom("modal-login2.jade",$scope, #here is where I pass the controller's scope as the scope for the Modal
  keyboard: false
  backdrop: 'static'
  )

)

Then my jade

div
  script(type='text/ng-template' id="modal-login2.jade")
    .modal-header
      h3.modal-title Login into Dashboard
    .modal-body
      form(name="loginForm", novalidate).form-horizontal
          .row.alert.alert-danger(ng-show='invalidPassword')
            span(translate) The specified e-mail or password was invalid !
          .row.form-group
            label.col-xs-3.control-label(for='email') E-mail
            .col-xs-9
              input.form-control(required, ng-model='email', type='email', name='email', placeholder="{{ placeholder.email }}")
          .row.form-group
            label.col-xs-3.control-label(for='password') Password
            .col-xs-9
              input.form-control(required, ng-minlength=6, ng-model='password', type='password', name='password', placeholder="{{ placeholder.password }}")
    .modal-footer
      .col-xs-offset-2.col-xs-3
        button.btn.btn-primary(type='submit', translate,
          ng-click='login(email, password)', ng-disabled='loginForm.$invalid || waitText') Sign in

So when trying to access the scope of the Modal I needed to use this instead of $scope.

So instead of

$scope.model

I should use

this.model

I am not entirely sure why but I think it has to with the Modal service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM