0

I am new to angular and I am trying to list my data in database .However I am gettin $scope not defined error..This is my code

 productsService
           .getProducts()
           .success(function (data, status, headers, config) {

               $scope.products = data;

               console.log($scope.products);
           })
           .error(function (error) {
               //Showing error message 
               $scope.status = 'Unable to retrieve product' + error.message;
           });

In my product Service I have

return {
            getProducts: function () {

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

            },

I am just getting failed alert. Please help!!!In backend I am able to get the data from database.

2 Answers 2

1

In an Angular service, you do not have access to the $scope, that is something you only have in directives and controllers. That is why you are getting an error about $scope being undefined.

Also, in your service you are returning a promise from your getProducts() method, yet you are also adding success and error handlers on to it. You should make up your mind whether you want to return the raw $http promise, or if instead you want to return a $q promise which is resolved with some transformed copy of the data returned in the $http().success() handler.

One final thing, if you are seeing the "failed" alert, that means your server is returning an error when you submit a request to /api/Products. If you go to that URL in your browser, does it work? You should look into why a basic GET request to that URL is not working.

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

1 Comment

i tried directly accessing the api/products and I am getting some error which I have no clue of Error> <Message>An error has occurred.</Message> <ExceptionMessage> The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'. </ExceptionMessage> <ExceptionType>System.InvalidOperationException</ExceptionType> <StackTrace/>
1

You should not uses scope variables in your service, you service should only be used to get/update/share some data.

Here is how your service should look like

Service

return {
    getProducts: function() {
        return $http({
            method: 'GET',
            url: '/api/Products'
        });
    },

and in your controller for that service method you can have a .success() and .error() which you can use to set your error messages.

Controller

productsService
   .getProducts()
   .success(function (data, status, headers, config) {
       $scope.products = data;
   })
   .error(function (error) {
       $scope.status = 'Unable to retrieve product' + error.message;
   });

Hope this helps.

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.