0

I'm trying to bind and show the value of array. When I'm assigning the value with the scope variable like this:

$scope.StockList = obj.data

It's working fine but when I'm pushing the value inside the array like this

$scope.StockList.push(obj.data);

It is not working as expected

Here I'm stuck with ng-repeat and scope variable. Can any one help me here is my effort.

$scope.StockList = [];
$scope.IsVisible = false;
var Stocks = [];

function GetStockEntries(loid, pid) {
  var data = { LocationId: loid, ProductId: pid }
  return $http.post(serviceURL + "/GetLocationStockEntries", data).then(
    function success(data, status, headers, config) {
      var obj = JSON.parse(data.data.d)

      //working fine in case single array
      //$scope.StockList = obj.data
      $scope.StockList.push(obj.data);    
    },
    function error(data, status, headers, config) {
      return data
    }
  )
}

$scope.StockListing = function (item) {
  debugger
  $scope.IsVisible = !$scope.IsVisible
  console.log($scope.StockList)
}

ng repeat code

<table cellpadding="5" cellspacing="0" class="stocktransferdiv">
  <tr>
    <td colspan="4">
      <table cellpadding="5" cellspacing="0" data-ng-repeat="stockItem in StockList" data-ng-show = "IsVisible" data-ng-cloak width="100%">                                            
        <tr style="border-bottom: 1px solid #ddd; padding-bottom: 5px; margin-bottom: 5px; float: left;">
          <td>
            <input type="radio" name="groupName" data-ng-value="true" data-ng-model="stockItem.selected" data-ng-change="onTaskSelect(stockItem)" />
          </td>
          <td>
            <input type="text" data-ng-model="stockItem.UserInventoryItemID"disabled="" readonly="" style="border: none; background-color: white;">
          </td>
          <td>
            <input type="text" data-ng-model="stockItem.LotNumber" disabled="" readonly="">
          </td>
          <td>
            <!--<input type="text" data-ng-model="stockItem.QuantityOnHand" disabled="" readonly="">-->
            <span>{{stockItem.QuantityOnHand}}</span>
            <span>{{stockItem.UnitName}}</span>
          </td>
          <td>
            <input type="text" data-ng-model="stockItem.EnteredQuantity" >
          </td>
          <td>
            <input type="text" data-ng-model="stockItem.Description" disabled="" readonly="">
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>

Here is the result of that json

enter image description here

7
  • Show us the structure of obj.data Commented Oct 15, 2016 at 8:24
  • @Sajeetharan : Updated with the obj.data result Commented Oct 15, 2016 at 8:28
  • You need to push the object not the property, try something like this, $scope.StockList.push(obj); Commented Oct 15, 2016 at 8:29
  • @Sajeetharan : i also tried that. Commented Oct 15, 2016 at 8:32
  • working on official computer, i dont have the rights sir! Commented Oct 15, 2016 at 8:34

2 Answers 2

1

Your service returns an array of objects, you need to loop over them and add it to the array,

var obj = data.data.d; 
$scope.result = obj ;
$scope.result.forEach(function(key) { 
$scope.StockList.push(key); 
}) 
Sign up to request clarification or add additional context in comments.

Comments

1

just a little bit modification of accepted answer. it will work .try this one.

var obj = JSON.parse(data.data.d);
        $scope.result = obj.data;
        angular.forEach($scope.result, function (key) {
            $scope.StockList.push(key);
        })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.