![](/img/trans.png)
[英]AngularJS scope variables doesn't update when it changes inside another function
[英]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.