我有些服務。 我想調試它並查看該服務中變數的狀態。 我的代碼是 below ( 解釋 below 代碼):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 ="
data.current = {{ data.current }}
data.total = {{ data.total }}
data.unlocked= {{ data.unlocked }}
";
$(".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');
});
我有一些 controller mainController的應用。 我想在其中使用服務 someService 。 我正在使用 push 方法。 這個方法將數據從 name 參數推到數據服務 Data的total 和 current 數組。 就像你看到的,我從 mainController 調用了方法 initDebug(),所以希望在屏幕上顯示調試窗口。 我想顯示數組 current,total 和 Data 數據服務的unlocked 。
實際上,窗口出現在屏幕上,但我在上面看到了它:data.current = {{ data.current }}
data.total = {{ data.total }}
data.all = {{ data.all }}
所以,AngularJS的模板沒有被插值。 如何使它們插值+ 我需要在這個 block ( 就像angularJS實際做的那樣) 中實時更新它們。
更新
使用 $compile.
所以我需要用這種方式改變我的initDebugMode 函數?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 ="
data.current = {{ data.current }}
data.total = {{ data.total }}
data.unlocked= {{ data.unlocked }}
";
$compile(template)(scopeImitation);
$(".someClass>. childClassOfSomeClass").append(template);
};
//etc...
]);
如何將變數傳遞到 $compile 服務中的2nd 括弧? 我正確使用了這個?