$ scope变量不在$ http.success内更新

时间:2015-09-01 04:28:27

标签: angularjs ng-grid

我从$ scope.targetHeaderList获取动态Header,我试图动态分配列名和数据,以便在ng-grid中显示它,但是$ scope.columns.push在成功内部没有工作。但是我得到了我在成功之外初步确定的列的ng-grid。

app.controller('customersCtrl',function($scope, $http) {
  $scope.loading = true;
  $scope.columns=[
    { field:'part', displayName:'Part '},
    { field:'description', displayName:'Description', pinnedLeft:true },
    { field:'cus', displayName:'CC', }
  ];

  $scope.columns.push({field:field,displayName:display});

  $scope.add = function (headerCollection) {
    for(var i=0, j=0;i < headerCollection.length; i++) {
      if (i%2 == 0) {
        fieldString = "cgDetailsList["+Math.floor(j)+"].cgHTS";
      } else {
        fieldString = "cgDetailsList["+Math.floor(j)+"].dutyRate";
      } 
      var displayString = $scope.headerCollection[i].toString();
      var obj = new tempObject(fieldString, displayString);
      console.log(fieldString +"  "+ displayString);
      $scope.columns.push(obj);
      j = j + 0.5;
    };

    $scope.columns.push({field:'createdBy',displayName:'Created By'});  
    $scope.columns.push({field:'requestor',displayName:'Requested'});
    $scope.columns.push({field:'trainedFlag',displayName:'Trained'});
    $scope.columns.push({field:'recCommited',displayName:'Records Committed'});

    $scope.$watch('columns', function(){});
};

$scope.data = '';
$http.get("autoAuditTargetData.do?parameter=targetData")
  .success(function(response) {
    $scope.rowTargetDataList = response.targetDataList;
    $scope.data = response.targetDataList;
    console.log($scope.data);
    $scope.headerCollection = response.targetHeaderList;
    console.log($scope.headerCollection);
    $scope.add($scope.headerCollection);                
    $scope.targetDataCount = response.targetDataCount;
    $scope.itemsPerPage = response.targetDataCount;
    $scope.targetHeaderStatic = response.targetHeaderStatic;
    $scope.targetHeaderDynamic = response.targetHeaderDynamic;
    $scope.queryID = response.queryID;
    $scope.auditCommitFlag = response.auditCommitFlag;

    if ($scope.auditCommitFlag != null) {
      if ($scope.auditCommitFlag != 'YES') {
        //document
        document.getElementById("commitAudit").disabled = true;
        document.getElementById("commitAudit").className = "buttondis";
      } else {
        //document
        //alert( " false ");
        document.getElementById("commitAudit").disabled = false;
        document.getElementById("commitAudit").className = "buttonpos";
      }
    }
    $scope.loading = false;
});

//$scope.columns.push({field:'cgDetailsList[0].cgHTS',displayName:'BR HTS'});   

console.log($scope.columns);

$scope.gridOptionsTarget = {
  enableSorting: true,
  columnDefs: $scope.columns,
  enablePinning: true,
  enableSorting: true,
  enableCellSelection: false,
  enableRowSelection: false,
  enableColumnResize : true,                                        
  data:'data'
};
console.log($scope.columns);

0 个答案:

没有答案