U
U
uoziod2014-09-05 16:10:14
Angular
uoziod, 2014-09-05 16:10:14

How to create $scope in Service?

I have a service that receives a certain string. I have a cached template in $templateCache that I can access from the service.
My goal is to create a new $scope right in the service in order to pass the received text there, compile the template based on it using $compile , and add the result to the end of a certain element on the page.
It is clear that the use of ngRepeat in the controller suggests itself here , but due to some architectural reasons, I need to use the service.
Thought about directives but not googling about how to instantiate a directive in a service (omg).
If anyone has fresh ideas on how to more accurately and competently implement such a task, I will be extremely grateful for your participation!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
uoziod, 2014-09-05
@uoziod

var item = angular.copy($templateCache.get('template.html'));
var linkFn = $compile(item);
var $scope = $rootScope.$new(true);
$scope.variable = 'test';
var content = linkFn($scope);
console log(content);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question