I have some some service. I wanna to debug it and see state of variables in this service. My code is below (explanation below the code):
module.factory('Data', function () {
var current = [], unlocked = [], all = [];
return {
current: current,
unlocked: unlocked,
total: all
}
});
module.controller('debugPlaneController', ['$scope', 'Data', function ($scope, data) {
$scope.data = data;
}]);
module.factory('someService', ['Data', function (data) {
var initDebugMode = function () {
var style = "style='width: 500px; height: 100px; background-color: #ccc;";
var template = "
<div "+style+" ng-controller='debugPlaneController'>
<p>data.current = {{ data.current }}</p>
<p>data.total = {{ data.total }}</p>
<p>data.unlocked= {{ data.unlocked }}</p>
</div>
";
$(".someClass > .childClassOfSomeClass").append(template);
};
// some more methods, for example
var push = function (name) {
data.current.push(name);
data.all.push(name);
}
// etc
return {
initDebug: initDebugMode,
push: push
// some more methods
}
}]);
module.controller('mainController', ['$scope', 'someService', function($scope, someService) {
someService.initDebug();
someService.push('lala');
});
So, I have some app with controller mainController
. I want to use service someService
in it. I am using push
method. This method pushes data from name
argument to total
and current
arrays of Data service Data
. As you see, I called method initDebug()
from mainController
, so I want debug window to appear on my screen. I want to show state of arrays current
, total
and unlocked
of Data
data-service.
Actually, window appears on screen, but I see this on it:
data.current = {{ data.current }}
data.total = {{ data.total }}
data.all = {{ data.all }}
So, AngularJS templates wasn't interpolated. How I can make them interpolating + I need live update of them in this block (like angularJS actually do).
Update
Using $compile.
So I need to change my initDebugMode
function this way?:
module.factory('someService', ['Data', '$compile', function (data, $compile) {
var initDebugMode = function () {
var style = "style='width: 500px; height: 100px; background-color: #ccc;";
var scopeImitation = { data: data };
var template = "
<div "+style+" ng-controller='debugPlaneController'>
<p>data.current = {{ data.current }}</p>
<p>data.total = {{ data.total }}</p>
<p>data.unlocked= {{ data.unlocked }}</p>
</div>
";
$compile(template)(scopeImitation);
$(".someClass > .childClassOfSomeClass").append(template);
};
// etc...
]);
How exactly passing variable to 2nd brackets in $compile
service works? I used this correctly?