2

I have an Angular app which is supposed to display different pages depending on the resource in the following way:

  1. Given url: localhost/foo/456
  2. Call REST service (fooService) to get a descriptor for the foo.
  3. Based on the descriptor use an appropriate template.

Here's the config part (a bit of a pseudocode):

.config(['$routeProvider', '$locationProvider', 'fooService', function ($routeProvider, $locationProvider, fooService) {
    $locationProvider.html5Mode();
    var fooTemplateUrlSelector = function (routeParams) {
            var foo = fooService.getFoo(routeParams.fooId);
            switch (foo.TemplateId) {
            case 1:
                return 'Foo1.html';
            case 2:
                return 'Foo2.html';
            default:
                return 'Unsupported.html';
            }
        };

    $routeProvider.when('/foo/:fooId', { templateUrl: fooTemplateUrlSelector, controller: 'FooController' })

The problem is that in the config phase I don't have access to any service ('Unknown provider: fooService'). It seems to be an Angular constraint that in the config part you're allowed to only access 'static' things. I get it.

What's the best (in terms of best practices) solution to this problem?

The templates might differ a lot, so I don't want to have one html file with a lot of "if TemplateId == 1, then display_some_markup else if TemplateId == 2 then display_something_else".

2
  • 1
    you could make provider instead of making service which would be easily available in config phase Commented May 27, 2015 at 17:37
  • How is my provider supposed to have $http injected in order to make a REST call? Can you give me a link to an example? Commented May 27, 2015 at 23:55

1 Answer 1

1

This is must be in app.config() ? I preffer this way:

html:

 <div ng-controller="Ctrl" ng-init="init()">
    <div ng-if="foo.templateID == 1">
      // ng-include="foo1.html"
    </div>
    <div ng-if="foo.templateID == 1">
      // ng-include="foo2.html"
    </div>
</div>

controller:

function Ctrl($scope, fooService) {
    $scope.init = function() {
        $scope.foo = fooService.getFoo(routeParams.fooId);
    };
    // other functions and features
};
Sign up to request clarification or add additional context in comments.

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.