Drupal AngularJS ajax简单的ng-repeat更新

时间:2015-06-09 12:37:47

标签: ajax angularjs drupal drupal-7 drupal-ajax

我有问题替换我的ng-repeat 我在ng-app中的html

<a href="#proceed" ng-click="order.submitOrderAjax()">
    <?php print t('Finish order'); ?>
</a>

<ul>
    <li ng-repeat="error in order.errors">{{ error.text }}</li>
</ul>

controller app.js

app.controller('orderController', function() {
this.errors = [];
this.submitOrderAjax = function(){
 var data = {} // some data here like name, phone
 jQuery.ajax({
    type: 'POST',
    url: Drupal.settings.basePath + 'ajax/submit_cart',
    data: { 'order': data },
    dataType: 'json',
    success: function(response){
        if(response.status == true){
            var link = response.link.replace(/\\\//g, "/");
            window.location.href = link;
        }else{
            this.errors = response.errors;
            console.log(this.errors);
        }   
    },
 });
};

console.log返回我需要的但ng-repeat没有更新

1 个答案:

答案 0 :(得分:0)

实际上html在响应之前渲染,所以你需要在响应之后渲染ng-repeat

<a href="#proceed" ng-click="order.submitOrderAjax()">
    <?php print t('Finish order'); ?>
</a>

<ul ng-if="flag">
    <li ng-repeat="error in order.errors">{{ error.text }}</li>
</ul>

控制器

    app.controller('orderController', function($scope) {
    this.errors = [];
$scope.flag=false;
    this.submitOrderAjax = function(){
        var data = {} // some data here like name, phone
        jQuery.ajax({
            type: 'POST',
            url: Drupal.settings.basePath + 'ajax/submit_cart',
            data: { 'order': data },
            dataType: 'json',
            success: function(response){
                if(response.status == true){
                        var link = response.link.replace(/\\\//g, "/");
                        window.location.href = link;

                }else{
                    this.errors = response.errors;
                    console.log(this.errors);
$scope.flag=true;
                }   
            },
          });
        };