3 * @name ui.router.util.$templateFactory
6 * @requires $templateCache
10 * Service. Manages loading of templates.
12 $TemplateFactory.$inject = ['$http', '$templateCache', '$injector'];
13 function $TemplateFactory( $http, $templateCache, $injector) {
17 * @name ui.router.util.$templateFactory#fromConfig
18 * @methodOf ui.router.util.$templateFactory
21 * Creates a template from a configuration object.
23 * @param {object} config Configuration object for which to load a template.
24 * The following properties are search in the specified order, and the first one
25 * that is defined is used to create the template:
27 * @param {string|object} config.template html string template or function to
28 * load via {@link ui.router.util.$templateFactory#fromString fromString}.
29 * @param {string|object} config.templateUrl url to load or a function returning
30 * the url to load via {@link ui.router.util.$templateFactory#fromUrl fromUrl}.
31 * @param {Function} config.templateProvider function to invoke via
32 * {@link ui.router.util.$templateFactory#fromProvider fromProvider}.
33 * @param {object} params Parameters to pass to the template function.
34 * @param {object} locals Locals to pass to `invoke` if the template is loaded
35 * via a `templateProvider`. Defaults to `{ params: params }`.
37 * @return {string|object} The template html as a string, or a promise for
38 * that string,or `null` if no template is configured.
40 this.fromConfig = function (config, params, locals) {
42 isDefined(config.template) ? this.fromString(config.template, params) :
43 isDefined(config.templateUrl) ? this.fromUrl(config.templateUrl, params) :
44 isDefined(config.templateProvider) ? this.fromProvider(config.templateProvider, params, locals) :
51 * @name ui.router.util.$templateFactory#fromString
52 * @methodOf ui.router.util.$templateFactory
55 * Creates a template from a string or a function returning a string.
57 * @param {string|object} template html template as a string or function that
58 * returns an html template as a string.
59 * @param {object} params Parameters to pass to the template function.
61 * @return {string|object} The template html as a string, or a promise for that
64 this.fromString = function (template, params) {
65 return isFunction(template) ? template(params) : template;
70 * @name ui.router.util.$templateFactory#fromUrl
71 * @methodOf ui.router.util.$templateFactory
74 * Loads a template from the a URL via `$http` and `$templateCache`.
76 * @param {string|Function} url url of the template to load, or a function
78 * @param {Object} params Parameters to pass to the url function.
79 * @return {string|Promise.<string>} The template html as a string, or a promise
82 this.fromUrl = function (url, params) {
83 if (isFunction(url)) url = url(params);
84 if (url == null) return null;
86 .get(url, { cache: $templateCache, headers: { Accept: 'text/html' }})
87 .then(function(response) { return response.data; });
92 * @name ui.router.util.$templateFactory#fromProvider
93 * @methodOf ui.router.util.$templateFactory
96 * Creates a template by invoking an injectable provider function.
98 * @param {Function} provider Function to invoke via `$injector.invoke`
99 * @param {Object} params Parameters for the template.
100 * @param {Object} locals Locals to pass to `invoke`. Defaults to
101 * `{ params: params }`.
102 * @return {string|Promise.<string>} The template html as a string, or a promise
105 this.fromProvider = function (provider, params, locals) {
106 return $injector.invoke(provider, null, locals || { params: params });
110 angular.module('ui.router.util').service('$templateFactory', $TemplateFactory);