繁体   English   中英

AngularJS在调用函数时不会更新作用域吗?

[英]Angularjs doesn't update scope when calling a function?

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

我尝试使用$ 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

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

所以代替

$scope.model

我应该用

this.model

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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