0

when i remove ng-controller from view ,resolve property works perfectly fine but when i put ng-controller in template,it throws unknown provider.can any body help me out why this is happening??

app.config(['$routeProvider',function($routeProvider) {
        $routeProvider.
            when('/', {
                templateUrl: 'page/login.html',
                controller: 'loginController',
                resolve: {
                    message: function(demoService){
                        return demoService.getResolveContent();
                    },
                    greeting: function(demoFactory){
                        return demoFactory.getGreeting();
                    }
                }

            }).
            when('/profile', {
                templateUrl: 'page/profile.html',
                controller: 'profileController'
            })
            .otherwise({
                redirectTo: '/'
            });
    }]);

function loginController($scope,$location,message,greeting){

    $scope.tempfactory = greeting;

}
<div style="color: white;width: 100%;height: 100%;background-color: lightgray" ng-controller="loginController">

    <p>{{tempfactory}}</p>
    <input type="text" ng-model="inputBoxValue">
    <button style="width: 100px;height: 100px" ng-click="profilePage()"></button>

</div>

If i replace above html with this:

<div style="color: white;width: 100%;height: 100%;background-color: lightgray" >

    <p>{{tempfactory}}</p>
    <input type="text" ng-model="inputBoxValue">
    <button style="width: 100px;height: 100px" ng-click="profilePage()"></button>

</div>

It worked perfectly fine.can any body tell me the reason of this.

0

1 Answer 1

2

Dependencies added using resolve are injected as part of route being resolved and controller being created and are not available otherwise. These are not global dependencies like a service or filter.

If you use ng-controller the route transition does not happen and route related dependency resolution and controller creation does not take place and the DI error is raised.

If you put controller in route definition and using ng-controller, two controller instances are created, but one gives DI error.

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

1 Comment

Thanks man, this really help me to understand why my dynamic created controller return unresolved DI, which I manage to solve it with creating a child/different controller not dependent to the resolve provider

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.