Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am trying to retrieve some sample values from mysql database and display it to my view using angularjs. I am pretty new to angular and I am trying to use $http.get to get my data. The following is my code

Angular Code

angular
.module('sampleApp.service.product', [])

.factory('productsService', [ '$http', function ($http) {

        return {
            getProducts: function () {


             //   return $http.get("/Home/GetTweets");
                return $http({
                    method: 'GET',

                    url: '/api/Products'
                }).success(function (data) {
                    alert("success");
                    alert(data);
                }).error(function (error) {
                    //Showing error message 
                    alert("failed");
                    $scope.status = 'Unable to retrieve products' + error.message;
                });

alert(data) returns [object,Object]

My Controller code has the following

 public class productsController : ApiController
{
    public IEnumerable<Product> Get()
    {
        ProductEntities context = new ProductEntities();
        var _products = from p in context.Products.AsEnumerable() select p;

        return _products;
    }

}

On debug I am getting the values in DB int the _products. Please help me out.Thanks In advance! enter code here

share|improve this question
    
You can use ngResource module to interact with REST(-like) API. Here is a good article on how to use it. Take a look: sitepoint.com/creating-crud-app-minutes-angulars-resource –  Karlen Kishmiryan Oct 15 '14 at 6:15

1 Answer 1

up vote 0 down vote accepted

your code is successful as i can see

put console.log(data); instead of alert(data); and then you can see the data in the browser console.

.factory('productsService', [ '$http','$q', function ($http,$q) {

    return {
        getProducts: function () {

             var deferred = $q.defer();

            $http({
                method: 'GET',
                url: '/api/Products'
            }).success(function (data) {

                deferred.resolve(data);

            }).error(function (error) {

                deferred.reject(data);

            });

            return deferred.promise;
        }
     }

In the Conteroller,

productsService.getProducts().then(function(data) {
     //success
     $scope.products = data.rows; 
     console.log($scope.products);
}, function() {
     //flier
});
share|improve this answer
    
Thank you @Karlen..I will check [email protected] I am getting the data properly in console..however the place where i am calling the service the data is undefined. productsService .getProducts() .success(function (data, status, headers, config) { $scope.products = data.rows; // alert(data.rows); console.log($scope.products); }); } –  user2281415 Oct 15 '14 at 6:28
    
please post that code also in your question –  K.Toress Oct 15 '14 at 6:31
    
you have to use promises in this case. i updated my answer please check. –  K.Toress Oct 15 '14 at 6:37
    
$scope.products has the value from db as its seen correctly in console. However its not getting displayed in my view. This is my code in view ` <tbody> <tr ng-repeat="product in products"> <td>{{product.Id}}</td> <td>{{product.Name}}</td> <td>{{product.Unit}}</td> ` –  user2281415 Oct 15 '14 at 6:39
    
any console errors ? –  K.Toress Oct 15 '14 at 6:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.