Angularjs在调用函数时不会更新范围?

时间:2014-10-13 08:46:00

标签: javascript angularjs coffeescript pug

我有一个我用于模态的模态服务,在服务中我传递了控制器的范围,以便模态视图可以访问范围对象。当我使用函数更新范围时,值永远不会更新。

我尝试过使用$ apply但是我得到的错误是摘要循环已经在运行。

所以这是代码

.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'
  )

)

然后我的玉

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

1 个答案:

答案 0 :(得分:0)

因此,当尝试访问Modal的范围时,我需要使用 this 而不是$ scope。

所以而不是

$scope.model

我应该使用

this.model

我不完全确定为什么,但我认为它必须与Modal服务。