2015-04-23 2 views
0
angular.module('cmfApp').controller('InventoryAddCtrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout){ 

    $scope.submit = function() { 
     var postData = { 
      ihenterprise_logisticsbundle_stockItem: { 
       name: $scope.formData.name, 
       itemNo: $scope.formData.itemNo 
      } 
     } 

     $http({ 
      method : 'POST', 
      url  : Routing.generate('ih_enterprise_api_stock_item_new'), 
      data : $.param(postData), // pass in data as strings 
      headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
     }) 
     .success(function(data) { 
      $scope.stockItems = $scope.stockItems.concat(data); 
      //console.log($scope.stockItems); This logs with the new item 
     }).error(function(error) { 
      console.log(error); 
     }); 
    }; 

}]); 

Вид просто не обновляется, когда я конкатенирую массив массива, если я пытаюсь вызвать $ scope. $ Apply(); после concat, я получаю дайджест в процессе, я также попытался использовать setTimeout, не помогает.AngularJS просмотр не обновляется после http-запроса

Вот HTML (Прут):

{% block listTable %} 
    <table class="table table-condensed table-expanding"> 
     <thead> 
     <tr> 
      <th>&nbsp;</th> 
      <th>Id</th> 
      <th>Created At</th> 
      <th>Navn</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat-start="stockItem in stockItems" data-toggle="collapse" data-target="#stockItem_{{ '{{stockItem.id}} '}}" class="accordion-toggle"> 
      <td> 
       <button class="btn btn-default btn-xs"><span class="glyphicon glyphicon-eye-open"></span></button> 
      </td> 
      <td>{{ '{{stockItem.id}} '}}</td> 
      <td>{{ '{{stockItem.created_at}} '}}</td> 
      <td>{{ '{{stockItem.name}} '}}</td> 
     </tr> 
     <tr ng-repeat-end=""> 
      <td colspan="6" class="hiddenRow"> 
       <div class="accordian-body collapse" id="package_{{ '{{stockItem.id}} '}}"> 
        test 
       </div> 
      </td> 
     </tr> 
     </tbody> 
    </table> 
{% endblock %} 

Контроллер InventoryAddCtrl находится вне блока, и исходные данные правильно применены при обновлении страницы.

+0

Я считаю, что проблема с кнопкой 'type', по умолчанию он является публичным, он должен' тип = «кнопка» 'кнопка будет выглядеть' <кнопка тип =» кнопка»класс =„БТН БТН-умолчанию БТН-хз“> ' –

+0

вам нужно использовать $ применять –

ответ

0
angular.module('cmfApp').controller('InventoryAddCtrl', ['$scope', '$http', '$timeout', function($scope, $http, $timeout){ 

$scope.submit = function() { 
    var postData = { 
     ihenterprise_logisticsbundle_stockItem: { 
      name: $scope.formData.name, 
      itemNo: $scope.formData.itemNo 
     } 
    } 

    $http({ 
     method : 'POST', 
     url  : Routing.generate('ih_enterprise_api_stock_item_new'), 
     data : $.param(postData), // pass in data as strings 
     headers : { 'Content-Type': 'application/x-www-form-urlencoded' } // set the headers so angular passing info as form data (not request payload) 
    }) 
    .success(function(data) { 
//You need to use $apply here. 
    $timeout(function(){ 
     $scope.$apply(function(){  
     $scope.stockItems = $scope.stockItems.concat(data); 
     }); 
     }); 
     //console.log($scope.stockItems); This logs with the new item 
    }).error(function(error) { 
     console.log(error); 
    }); 
}; 

}]); 
+0

я мог бы быть неправильно в синтаксисе. Проверьте, применяется ли документация с использованием $. –

+0

Я считаю, что синтаксис для '$ apply' правильный, но если бы это было не так, ** вы **, так как ответчик должен проверить синтаксис и отправить синтаксически правильный ответ, а не размещать синтаксически неправильный ответ и сообщать asker посмотреть синтаксис – Tom

+0

Да. Я бы сделал это, если бы получил достаточную информацию. Все, что я помню, это использовать $ apply, решит проблему. И желаю этого. –

-1

Я не уверен, но я думаю, вы не определили $scope.stockItems, как пустой массив.

$scope.stockItems=[]; просто положить чуть выше $scope.submit() функции

Смежные вопросы