0

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!

1

1 Answer 1

0

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
});
Sign up to request clarification or add additional context in comments.

4 Comments

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); }); }
you have to use promises in this case. i updated my answer please check.
$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> `
yeah great :) glad if u can upvote and mark as the correct answer if its solve ur issue :)

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.