-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngReplace.js
54 lines (45 loc) · 1.47 KB
/
ngReplace.js
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
45
46
47
48
49
50
51
52
53
54
(function (root, factory) {
if (typeof module !== 'undefined' && module.exports) {
// CommonJS
module.exports = factory(require('angular'));
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['angular'], factory);
} else {
// Global Variables
factory(root.angular);
}
}(this, function (angular) {
'use strict';
var m = angular.module('ngReplace', []);
m.factory('replaceWithTpl',['$compile','$http',
function($compile,$http){
var replaceTpl = function(replaceTpl,replaceId,scope){
$http.get(replaceTpl, {cache: true}).then(function (response) {
var replaceDom = angular.element('#' + replaceId);
replaceDom.html(response.data);
$compile(replaceDom.contents())(scope);
});
}
return {
template: replaceTpl
}
}
])
m.directive('ngClickReplace', function (replaceWith) {
var linker = function (scope, element, attrs) {
element.bind('click', function () {
var replaceTpl = attrs.replaceTpl,
replaceId = attrs.replaceId;
replaceWithTpl.template(replaceTpl,replaceId,scope);
})
};
return {
restrict: 'E',
link: linker,
scope: {
content: '='
}
};
});
}));