0

I'm new in Angular, and I'm trying to understand the basic My Project is like Mean.

My router.js

router.get('/', function (req, res, next) {
    res.render('index', {});
});

my Angularfile.js

var app = angular.module('ibApp', ['ngRoute','ui.router']);

app.config([
    '$stateProvider',
    '$urlRouterProvider',
    function ($stateProvider, $urlRouterProvider) {
        $stateProvider.state('home', {
            url: '/',
            resolve: {
                foo: [function () {
                    console.log('inside')
                }]
            }
        });

        $urlRouterProvider.otherwise('home');
    }]);



app.controller("MyCtrl",['$scope',function($scope) {
    console.log("here!");
}]);

my index.html

 <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.1/angular-route.min.js"></script>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.js"></script>
    <script src="/javascripts/angularApp.js"></script>

But when I get the / (index) route, I haven't console.log('inside'), but I have only console.log("here");

I tried also http://localhost:3000/#/, but my problem persist.

10
  • remove [ ] from the function. Commented Jul 2, 2015 at 9:52
  • in resolve? I tried, but I have the same problem Commented Jul 2, 2015 at 9:54
  • resolve is returning the function to your controller, not the result of the function, and since the function isn't named, you don't have any way to execute the function in the controller. add () to ensure the function executes. Commented Jul 2, 2015 at 9:56
  • Alert() doesn't work neither? Commented Jul 2, 2015 at 9:58
  • @Claies am I missing something? resolve does run the function, see the docs: "if function, then it is injected and the return value is treated as the dependency" Commented Jul 2, 2015 at 9:59

1 Answer 1

2

The Resolve provider is not executing the function in your case. The function itself is being returned as the dependency object, because the function is never invoked. If the function is invoked, the return value of the function is treated as the dependency.

try:

resolve: {
    foo: [function () {
            console.log('inside')
          }()]
    }

note the () at the end of the function declaration to invoke the function.

You can also return the function with a name, and invoke the function inside the controller.

Note that the reason this is occurring is because foo is not being provided the function as the resolve object (which would implicitly execute the function and assign the result to foo), rather foo is declared as an array, and the function is simply foo[0]. You could also invoke the function as foo[0] in the controller.

Based on your comments, you are trying to execute a service function and return the result. In this case, you should ensure the service is available to the module, and then simply invoke it. It is not necessary to try to inject the service into your resolve function the way you might with a controller declaration.

i.e. instead of doing :

resolve: { 
    postPromise: ['users', function (users) {
        console.dir('here '+users);
        return users.getAll();
    }]

you can do:

app.config([
'$interpolateProvider',
'$stateProvider',
'$urlRouterProvider',
'users',
function ($interpolateProvider, $stateProvider, $urlRouterProvider, users) {
...

resolve: { 
    allTheUsers: function (users) {
        console.dir('here '+users);
        return users.getAll();
}

http://plnkr.co/edit/FOgVgz5kvfuzPAAgtsVC?p=preview

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

7 Comments

My problem is little bit complicated. I want get the data in resolve, after call my service with $http. You can find the code here pastebin.com/PDN9Arkd. my resolve doesn't call my service, and inside my controller, I don't have my data.
again, you are returning postPromise as an array, with 'users' as postPromise[0], and your function as postPromise[1]. this isn't going to invoke the function, nor is it going to supply users as a parameter for the function (it's not a dependency injection array).
@monkeyUser I updated my answer with what I think you are probably trying to achieve.
Thanks, again. I'm trying your code , but I don't see 'here'+user
I updated the function to show where to inject users.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.