angularjs – angular.js范围变量正在更新但视图不是

前端之家收集整理的这篇文章主要介绍了angularjs – angular.js范围变量正在更新但视图不是前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用angular.js与rails作为我的后端,我在更新视图内的范围变量的值时遇到问题.在这里,我试图创建一个购物车类的东西.

HTML代码

<div class="modal hide fade" id="orderForm">
<div class="modal-header">
    <h3>
        Please enter your location details      <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    </h3>
</div>
<div class="modal-body scrollable">
    <form accept-charset="UTF-8" class="simple_form form-horizontal" name="orderForm" novalidate="novalidate" ng-submit="validateAndSubmit(orderForm)"  id="order_form">
        <div class="control-group string optional order_full_name">
            <label class="string optional control-label" for="order_full_name">Full name</label>
            <div class="controls">
                <input class="string optional required  " id="order_full_name" name="full_name" ng-model="full_name" placeholder="eg. Kapil Sharma" size="50" type="text" value="">
                </div>
            </div>


</div>
<div class="modal-footer">
    <a href="" data-dismiss="modal" aria-hidden="true" class="btn">
        Close
    </a>
    <button type="submit" class="btn btn-success"> <i class="icon-ok"></i>&nbsp;Place order</button>
    <%# end %>
</form>
</div>

这是一个bootstrap模态,用$(‘#orderForm’)调用.modal(‘show’);

这是我的javascript角度代码.

$scope.validateAndSubmit = function(form){          
if(form.$valid){
    var formData = $('#order_form').serializeObject();; 

    $http.post('/create_order',{'order': formData})
    .success(function(data,status){
        $scope.currentOrder = data;
        $('#orderForm').modal('hide');
        $scope.currentOrder = [];
    }).error(function(data,status){
    //show error
        alert("error");
    });
}
else{
    alert("invalid form");
}

}

问题是我的范围变量$scope.currentOrder,在所有其他函数中它工作正常,但在validateAndSubmit方法中它没有按需要更新视图我已经使用javascript setInterval方法检查了范围变量的值

setInterval(function(){
        alert($scope.currentOrder);
    },5000);

更新的值在警报中很好,但视图没有更新值,请帮助.提前致谢.

解决方法

看来回调是在范围之外调用的,你需要添加$scope.$apply();在您的回调中,您希望它何时更新视图.

猜你在找的Angularjs相关文章