0

I was trying to do a webAPI call from AnguarJs. I receive success response "data" object perfectly.When it is passed to HTML page under "ng-repeat" it is not displaying any records.

Following is the one not working

        <tr ng-repeat = "cust in Customers">
            <td>{{ cust.CustomerName }} </td>
            <td>{{ cust.CustomerCode }}</td>
            <td>{{ cust.CustomerAmount }}</td>
            <td>{{ cust.ProcessDate }}</td>
        </tr>

But if i put in this way it will display the 0th index records

<tr ng-repeat = "cust in Customers">
    <td>{{cust[0].CustomerName }} </td>
    <td>{{cust[0].CustomerCode }}</td>
    <td>{{cust[0].CustomerAmount}}</td>
    <td>{{cust[0].ProcessDate }}</td>
 </tr>

Note : In the below code i split up files in different javascript files and referred in the main html page.Just for your information.

My Fiddle Link : JsFiddle

Please help me in resolve it.

function Utility(){
    this.ApplicationVersion = "0.0.1";
    this.ApplicationName = "AngularJs First Project";
    this.getDate = function () {
        var dt = new Date();
        return dt.toDateString();
    }
    this.IsEmpty = function (value) {
        if (value.length == 0) {
            return false;
        }
        else {
            return true;
        }
    }
}

function Customer(utility) {
    this.CustomerCode = "1001";
    this.CustomerName = "Ragu";
    this.CustomerAmount = 100;
    this.CalculateDiscount = function()
    {
        return 10;
    }
    this.ProcessDate = utility.getDate();

}

function Factory()
{
    return {
        CreateCustomer: function (utility) {
            return new Customer(utility);

        }
    }
 }
 
 /// <reference path="Utility.js" />
/// <reference path="Customer.js" />
var myApp = angular.module("myApp", []);

myApp.controller("BindingCode",BindingCode);
myApp.factory("Factory", Factory);
myApp.service("UtilityObj", Utility);
function BindingCode($scope, UtilityObj, Factory,$http)
{
    $scope.Customer = Factory.CreateCustomer(UtilityObj);
    $scope.Customers = [];
    $scope.Utility = UtilityObj;
    $scope.Customer.CustomerCode = "1002";
    $scope.Customer.CustomerName = "Raman";
    $scope.Customer.ProcessDate = UtilityObj.getDate();
    $scope.Color = "blue";
    $scope.$watch("Customer.CustomerAmount", function () {
        if ($scope.Customer.CustomerAmount < 1000) {
            $scope.Color = "Red";
        }
        else {
            $scope.Color = "Green";
        }
    });
    $scope.Submit = function()
    {
        debugger
        if ($scope.Utility.IsEmpty($scope.Customer.CustomerAmount)) {
            debugger
            
            $http.post("http://localhost:61860/api/Customer", $scope.Customer).then(function(data){
                $scope.Customers = data;
                debugger
                    $scope.Customer = {}; // clearing the record
            },
            function(data)
            {
                debugger
                alert("inside error http call" + data);
            }
            );
            
            //$http.post("http://localhost:61860/api/Customer", $scope.Customer).
            //    success(function (data) {
            //    debugger
            //    $scope.Customers = data;
            //    $scope.Customer = {};
            //});
        }
        else {
            alert("No Proper Date");
        }
    }
}
<script src="Scripts/angular.js"></script>
<script src="Customer.js"></script>
<script src="Utility.js"></script>
<script src="app.js"></script>
<body ng-app="myApp">
    <div id="CustScreen" ng-controller="BindingCode">
        CustomerCode : <input type="text" ng-model="Customer.CustomerCode" id="txtCustomercode" /> <br />
        CustomerName : <input type="text" ng-model="Customer.CustomerName" id="txtCustomerName" /> <br />
        CustomerDate : <input type="text" ng-model="Customer.ProcessDate" id="txtCustomerDate" /> <br />
        CustomerAmount : <input type="text" style="background-color:{{ Color }}" ng-model="Customer.CustomerAmount" id="txtCustomerAmount" /><br />
        <br />
        {{ Customer.CustomerCode }} <br />
        {{ Customer.CustomerName }} <br />
        {{ Customer.ProcessDate }}<br />
        {{ Customer.CustomerAmount}} <br />
        <input type="submit" ng-click="Submit()" id="Submit" />
        <table>
            <tr>
                <td>Name</td>
                <td>Code</td>
                <td>Amount</td>
                <td>ProcessDate</td>
            </tr>
            <tr ng-repeat = "cust in Customers">
                <td>{{cust.CustomerName }} </td>
                <td>{{cust.CustomerCode }}</td>
                <td>{{cust.CustomerAmount}}</td>
                <td>{{cust.ProcessDate }}</td>
            </tr>
            </table>
</div>      
</body> 
    

7
  • the point of jsfiddle is to reproduce your problem. Not just pasting the same code as in the question. You will have to include the minimal necessary code from your other files, and possibly loading angular from an external source. Can you show us how the json returned from your backend looks like ? It looks like every cust item is an array - are you returning an array of arrays? Commented Apr 26, 2017 at 12:43
  • you can also change the included js order and try it. and where is angular.min.js in your code?? Commented Apr 26, 2017 at 12:49
  • this can help you i guess.. http://stackoverflow.com/questions/31748612/angularjs-ng-repeat-array-of-arrays Commented Apr 26, 2017 at 12:53
  • I stripped out all of the extra stuff, but here's an example of it working with ng-repeat. jsfiddle.net/6u59q7b7 Commented Apr 26, 2017 at 13:07
  • Hi Ganzalo, i am new to angular and jsfiddle. I am not sure how the c# (ASP.Net WEB aPI) code will be taken into consideration in fiddle. That's why for better understanding i pasted the code there. Commented Apr 27, 2017 at 17:45

1 Answer 1

0

Try this $scope.Customers=data[0];

Sign up to request clarification or add additional context in comments.

3 Comments

i tried "$scope.Customers = data[0];" but still it is not listing
I don't know why the following is not working <tr ng-repeat = "cust in Customers"> <td>{{ cust.CustomerName }} </td> <td>{{ cust.CustomerCode }}</td> <td>{{ cust.CustomerAmount }}</td> <td>{{ cust.ProcessDate }}</td> </tr> But if i put in this way it will display the 0th index records <tr ng-repeat = "cust in Customers"> <td>{{cust[0].CustomerName }} </td> <td>{{cust[0].CustomerCode }}</td> <td>{{cust[0].CustomerAmount}}</td> <td>{{cust[0].ProcessDate }}</td> </tr>
Can you post here what you are getting as 'data' from $http ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.