0

I'm using AngularJS 1.6.1, and I'm trying to do a simple task that I have already done before (long time ago), but for some reason I can't realize what is happening this time.

Basically I want to call my REST service, but when I pass my factory in the parameters of the resolve function, it stops working.

I made a simple example to post here in order to make it easier:

index.html

<html ng-app="TesteApp">
<head>
  <script src="lib/angular/angular.js"></script>
  <script src="lib/angular/angular-route.js"></script>

  <script src="app.js"></script>
</head>
<body>
    <div class="ng-view">
    </div>
</body>

view.html

<h1>DONE</h1>

app.js

angular.module('TesteApp', ['ngRoute']);

angular.module('TesteApp').controller('testeController', function($scope, resultSet) {
   console.log("1");
});

angular.module('TesteApp').config(function ($routeProvider) {
    $routeProvider.when("/", {
        templateUrl: "view.html",
        controller: "testeController",
        resolve: {
            resultSet: function(callApi) { // seems like 'callApi' is not being recognized?!
                //If I return the fixed list below and take the 'callApi' parameter above, it works.
                //return "[{id:1, nome: 'teste'}, {id:2, nome: 'teste2'}]";
                return callApi.getResult(); 
            }
        }
    });
});

angular.module("TesteApp").factory("callApi", function ($http, config) {
    var _getResult = function () {
        return $http.get("http://localhost:8080/result");
    };

    return {
        getResult: _getResult
    };
});

Like I said in the comment, it seems like 'callApi' is not recognized.

The console does not print errors and the <div class="ng-view">is not replaced by <h1>DONE</h1>, and no request is in fact done.

5
  • What exactly is 'not recognized'? If you've got an error, pease, post it entirely. Commented Aug 10, 2017 at 13:07
  • @estus Do you have any idea, why confi dependency injection inside callApi factory fails silently in above code. It doesn't throw an error in console. I never saw this before :( Sorry for asking you here Commented Aug 10, 2017 at 13:11
  • 1
    @PankajParkar Hi. No problems. That's because the service is first injected in resolver and injection error in factory results in resolver rejection and router error, plnkr.co/edit/IS73Ux4yjaKVuMCR8U9q?p=preview . I don't think I encountered this before, but yes, it makes perfect sense, that's how I would expect it to work. I guess it makes sense to set up $routeChangeError handler in dev environment, thanks for drawing my attention to this, good to keep it in mind. Commented Aug 10, 2017 at 13:43
  • @estus great! great! great!, You always add good informative comments every time. I learnt something new today :) You're awesome as always. Thanks much :) Commented Aug 10, 2017 at 13:46
  • You're welcome. Glad to see you among Angular fellows on SO. Commented Aug 10, 2017 at 13:54

1 Answer 1

1

Update

Its wiered(as it isn't throwing any error in console), but config dependency injected inside callApi wasn't mentioned anywhere, so it is failing there. Removed that unknown dependency will fix your issue.

Demo Here


You had wrong service method name. Instead of getArticles call getResult.

return callApi.getArticles();

should be

return callApi.getResult();
Sign up to request clarification or add additional context in comments.

4 Comments

sorry, it was my mistake when I replaced a few things to post here. The problem still happens.
Can you please update code & exact error that you're getting
@BrunoBL check updated answer :) Hope it will help you :)
Right, for this example it solves. My original app actually has the 'config', and the problem still occurs.. even taking config. but for this simple example I did, it is working, so I will consider your answer and try figure out things in original app. Thank you :)

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.