How to route without a templateUrl?

后端 未结 2 625
耶瑟儿~
耶瑟儿~ 2021-02-01 15:39

Ok. I have a url setup to log a user out. On the server, there is no html. The session on the server simply gets destroyed, and then the user is redirected to an address.

<
2条回答
  •  天命终不由人
    2021-02-01 16:26

    A workaround is to use template instead of templateUrl. From the Angular docs:

    template – {string=} – html template as a string that should be used by ngView or ngInclude directives. this property takes precedence over templateUrl.

    This can be used as follows:

    $routeProvider.when("/foo", {template: " ", controller: "Ctrl"});
    

    Note: You must use " " instead of an empty string "" because Angular uses an if (template) check before firing the controller, and an empty string evaluates to false.

    -- EDIT --

    A better way to do it is to use the resolve map. See the Angular Docs:

    resolve - {Object.=} - An optional map of dependencies which should be injected into the controller.

    This can be used like this:

    $routeProvider.when('/foo', {resolve: {redirect: 'RedirectService'}});
    

    Note: I've changed it from "Ctrl" to "RedirectService", because what you're describing in the question isn't really a "controller" in the Angular sense. It doesn't set up scope for a view. Instead, it's more like a service, which ends up redirecting.

提交回复
热议问题