-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangularjs_dynamic_template.js
More file actions
44 lines (41 loc) · 1.5 KB
/
angularjs_dynamic_template.js
File metadata and controls
44 lines (41 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* This is directive loading dynamic templates based on the passed type.
* scope: @param type string : the type to identify which template to load.
*/
app.directive('dynamicTemplate', function ($compile, $templateCache, $http) {
/** Dynamically get the template by type. */
var getTemplate = function(contentType) {
var templateLoader,
baseUrl = 'views/partial/templates/',
templateMap = {
blog: 'blog.html',
photo: 'photo.html',
video: 'video.html'
};
/** Generating the link to load the template from. */
var templateUrl = baseUrl + templateMap[contentType];
templateLoader = $http.get(templateUrl, {cache: $templateCache});
return templateLoader;
};
/** The link function which will be used to dynamically load the template. */
var linker = function(scope, element, attrs) {
var loader = getTemplate(scope.type);
var promise = loader.success(function(html) {
element.html(html);
}).then(function (response) {
element.replaceWith($compile(element.html())(scope));
});
};
return {
link: linker,
restrict: "AE",
/** Add as many parameters you want to pass to the isolated scope HERE. */
scope: {
content: '=?',
type: '=?'
},
/** Here comes the logic/controller of the directive. */
controller: function ($scope) {
}
}
});