I am trying to create a directive that would load a template. The template will then be cached so the second time you click on the element it would not try to load it but instead get recently loaded value from $templateCache.
I am noticing that in case of the cache hit I don't get any response from $http.get() method.
<html ng-app="website">
<body ng-controller="MyController">
<a href='#' ng-click="load()">Click Event</a>
<a href='#' click-load>Click Directive</a>
</body>
</html>
angular.module('website', []).directive('clickLoad', function($q, $http, $templateCache) {
return function(scope, element, attrs) {
function loadData() {
$http.get('http://fiddle.jshell.net', {
cache: $templateCache
}).then(function(result) {
alert('loaded ' + result.data.length + " bytes");
});
}
element.bind('click', loadData);
};
});
function MyController($scope, $http, $templateCache) {
$scope.load = function() {
$http.get('http://fiddle.jshell.net', {
cache: $templateCache
}).then(function(result) {
alert('loaded ' + result.data.length + " bytes");
});
}
}
I've created a fiddle simulating my scenario:
Note that you can click Click Event link as many times as you want, however "Click Directive" link only works once if you clicked it first, it doesn't work at all if you click "Click Event" link first.
Any ideas are greatly appreciated.