, , . , , . , , , console.log($templateCache.get('snippet1.html')) undefined - , $http.get .
api $templateCache, - , ajax. , , , $templateCache
console.log($templateCache.info())
{id: "templates", size: 0}
JS ,
setTimeout(function() {
console.log($templateCache.info())
}, 1000);
{id: "templates", size: 2}
, ... . , -. $q $rootScope .run
myApp.run(['$templateCache', '$http', '$q', '$rootScope', function($templateCache, $http, $q, $rootScope){
$q.all([
$http.get('snippet1.html',{ cache : $templateCache }),
$http.get('snippet2.html',{ cache : $templateCache })
]).then(function(resp){
$rootScope.templateCache = resp
})
}]
);
, , var $rootScope $rootScope.templateCache $watch . $templateCache, , $rootScope.templateCache, , $q promises
link: function(scope, element, attrs) {
scope.$parent.$parent.$watch('templateCache', function(n, o) {
if(n) {
element.append($compile($templateCache.get('snippet1.html')[1])(scope));
}
});
}
! . scope.$parent.$parent , scope , , $rootScope.
, , ? ! , , , , . .
,
var providers = {};
var $injector = angular.injector(['ng']);
var myApp = angular.module('myApp', []);
$injector.invoke(function($http, $q, $templateCache, $document) {
$q.all([
$http.get('snippet1.html',{ cache : $templateCache }),
$http.get('snippet2.html',{ cache : $templateCache })
]).then(function(resp){
providers.cacheProvider = $templateCache;
angular.bootstrap($document, ['myApp']);
});
});
myApp
.controller('test',function() {
})
.directive('myTemplate', function ($templateCache, $compile) {
return {
restrict: 'EA',
scope: {
snippets: '='
},
link: function(scope, element, attrs) {
element.append($compile(providers.cacheProvider.get('snippet1.html')[1])(scope));
}
};
});