anth
anth

Reputation: 1

AngularJs - Fill table with $resource

I'm learning angularjs and I'm triyng to get a list of Object from WebApi. I've already tried to do it with $ http and it works fine:

// in controller
var listC = 'http://localhost:12345/api/WebApiTest/ContList';

$http({
    method: 'GET', url: listC
}).success(function (ContactsList) {
    $scope.contacts= ContactsList;
}).error(function () {
    alert("Error in List");
});

But I can not do the same with $resource, hereafter what I did: The table:

<tr ng-repeat="progetto in progetti | filter:progSearch | orderBy:orderByField:reverseSort">
   <td style="padding:10px;"><img src="images/user-2.png" href="#" width="25" ng-click="GetProgById(progetto.ID_Progetto)" data-toggle="modal" data-target="#progetto-modal" alt="Vista" /></td>
   <td style="padding:10px;"><img src="images/edit-user.png" href="#" width="25" ng-click="GetProgById(progetto.ID_Progetto)" data-toggle="modal" data-target="#progetto-update-modal" alt="Modifica" /></td>
   <td style="padding:10px;">{{progetto.NomeProgetto}}</td>
   <td style="padding:10px;">{{progetto.Descrizione}}</td>
   <td style="padding:10px;">{{progetto.NomeAreaRicerca}}</td>
   <td style="padding:10px;">{{progetto.Responsabile}}</td>
   <td style="padding:10px;"><img src="images/delete-user.png" href="#" width="25" ng-click="GetProgById(progetto.ID_Progetto)" data-toggle="modal" data-target="#modalEliminaProgetto" alt="Elimina" /></td>
</tr>

The module:

var app = angular.module('myApp', ['ngResource'])

.factory('progettiService', function ($resource) {

    return $resource('http://localhost:12345/api/WebApiTest/ProgettiList');
    // ProgettiList is specified below

});

The controller:

app.controller('progCtrl', function ($scope, progettiService) {

$scope.progetti = progettiService.query();

});

"ProgettiList" in api:

[HttpGet]
public IHttpActionResult ProgettiList()
{
    var listProg = logic.ProgettiList();
    return Ok(listProg);
}

in logic...

public List<ProgettoDAO> ProgettiList()
{
    return progDAL.ProgettiListDAL();
}

and in DAL:

public List<ProgettoDAO> ProgettiListDAL()
{
    List<ProgettoDAO> list = new List<ProgettoDAO>();

    string SC = ConfigurationManager.ConnectionStrings["Connection"].ToString();

    SqlConnection conn = new SqlConnection(SC);

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.CommandText = "SP_Progetti_List";

    cmd.Parameters.AddWithValue("@ID_Progetto", null);

    try
    {
        conn.Open();

        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            ProgettoDAO progetto = new ProgettoDAO();

            if (reader["ID_Progetto"] != System.DBNull.Value)
            {
                progetto.ID_Progetto = Convert.ToInt16(reader["ID_Progetto"]);
            }
            if (reader["NomeProgetto"] != System.DBNull.Value)
            {
                progetto.NomeProgetto = Convert.ToString(reader["NomeProgetto"]);
            }
            if (reader["Responsabile"] != System.DBNull.Value)
            {
                progetto.Responsabile = Convert.ToString(reader["Responsabile"]);
            }
            if (reader["Descrizione"] != System.DBNull.Value)
            {
                progetto.Descrizione = Convert.ToString(reader["Descrizione"]);
            }
            if (reader["ID_AreaRicerca"] != System.DBNull.Value)
            {
                progetto.ID_AreaRicerca = Convert.ToInt16(reader["ID_AreaRicerca"]);
            }
            if (reader["NomeAreaRicerca"] != System.DBNull.Value)
            {
                progetto.NomeAreaRicerca = Convert.ToString(reader["NomeAreaRicerca"]);
            }

            list.Add(progetto);
        }
        conn.Close();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
    finally
    {
        conn.Close();
        conn.Dispose();
    }
    return list;
}

The table should fill as soon as the page loads. However, the method returns a list Count = 0;

Upvotes: 0

Views: 81

Answers (0)

Related Questions