anjular 添加html,如何使用AngularJS模板向DOM添加一些HTML代码,并使AngularJS能够插入它们?_javascript_开发99编程知识库...

我有些服務。 我想調試它並查看該服務中變數的狀態。 我的代碼是 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 括弧? 我正確使用了這個?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值