0

我想要做的是從$ http請求中獲取JSON對象,請求在控制器內部,並構建一個指令,顯示JSON對象中的多個對象一個網格。更新控制器的變量指令的模板 - angularjs

問題是,當對象到達時,我必須在指令的控制器中處理它,以便能夠在模板中使用它,因此,當JSON對象更改時,它不會反映在模板中。如何讓指令瞭解對象的更改並強制重新加載模板?

// The Directive code 
amobile.directive('grid', function() { 
return { 
    restrict: 'E', 
    scope: { 
     break: '=break', 
     source: '=source' 
    }, 
    controller: function($scope) { 
     var source = $scope.source; 
     $scope.final_data = new Array(source.length); 
     if(source){ 
      for(var j=0; j < source.length; ++j){ 
       var total = Math.ceil(source[j]['Division'].length/$scope.break); 
       var data = new Array(total); 
       for (var i = 0; i < total; ++i) { 
        data[i] = source[j]['Division'].slice(i * $scope.break, (i + 1) * $scope.break); 
       } 
       $scope.final_data[j] = data; 
      } 
     } 

    }, 
    templateUrl:'directives/grid.tpl.html', 
    replace: true 
}; 
}); 

//The template 
<div ng-repeat="data in final_data"> 
<div layout="vertical" layout-sm="horizontal" layout-padding class="" ng-repeat="row in data"> 
    <div class="" ng-repeat="item in row"> 
     <div flex style="width:100px"> 
      {{item.name}} 
     </div> 
    </div> 
</div> 

​​

回答

0

最簡單的辦法是使用手錶

controller: function($scope) { 
    processData = function() { 
     var source = $scope.source; 
     $scope.final_data = new Array(source.length); 
     if(source){ 
     for(var j=0; j < source.length; ++j){ 
      var total = Math.ceil(source[j]['Division'].length/$scope.break); 
      var data = new Array(total); 
      for (var i = 0; i < total; ++i) { 
       data[i] = source[j]['Division'].slice(i * $scope.break, (i + 1) * $scope.break); 
      } 
      $scope.final_data[j] = data; 
     } 
     } 
    } 
    $scope.$watch('div.data.floors', processData, true) 

}, 
+0

,因爲它是不工作的代碼。當我更改對象並按下按鈕更新視圖時,我可以接收新對象(通過Firebug),但視圖不會更新。 – iara 2014-09-02 13:31:38

相關問題