一个指令中的模板取决于条件
templates in one directive depending on conditon
如何替换指令中的div标签?
例如,我有一个带有div 标签的面板:
<div class="panel panel-primary">
<div class="panel-heading">Heading</div>
<div class="panel-body">
<div class="whatever">
This tag has to be replaced in the directive after the counter is 10!
</div>
</div>
我有一个模板更改 Div.html
<div class="whatever2">
I am the new div-Tag!
</div>
这可能吗?如果是,如何?ng-show也是一种选择,但不是一个好的选择。我想为此标签定义许多模板。根据条件(变量(,应加载模板。在指令中,您可以加载模板,但是如何决定应该加载哪个模板?
例如,如果选中=真负载模板1-如果选中是假laod模板2
提前感谢
我正在使用眼动仪,并且有很多面板。通过眼睛位置,我检查用户正在看的指令。那么哪个面板是焦点:
myApp.directive('travelInformation', function ($rootScope, $interval) {
return {
restrict: 'A',
scope: {
updateMethod: '=',
},
link: function (scope, element, attrs) {
var routePanelCounter = 0;
var timeTablePanelCounter = 0;
var count = 10;
this.routePanelUpdate = function (element) {
routePanelCounter +=1
if (routePanelCounter == count) {
//replace the old tag with the new template1
}
};
this.timeTablePanelUpdate = function (element) {
timeTablePanelCounter += 1;
if (timeTablePanelCounter == count) {
//replace the old tag with the new template2
}
};
$interval(function () {
var boundingRect = element[0].getBoundingClientRect();
if ($rootScope.gazePoints){
for (var i = 0; i < $rootScope.gazePoints.length; i++) {
var x = $rootScope.gazePoints[i].x;
var y = $rootScope.gazePoints[i].y;
if (x >= boundingRect.left && x <= boundingRect.right && y >= boundingRect.top && y <= boundingRect.bottom) {
console.log("Has Focus");
this[scope.updateMethod](element);
}
}
}
}, 3000);
}
};
}(;
html如下所示:
<div class="panel panel-primary fixed-panel" travel-information data-update-method="'routePanelUpdate'">
<div class="panel-heading">test1</div>
<div class="panel-body">
old content /// replace the the div-tag with a new template
</div>
</div>
<div class="panel panel-primary fixed-panel" travel-information data-update-method="'timeTablePanelUpdate'">
<div class="panel-heading">test2</div>
<div class="panel-body">
<div>
Old content
</div>
</div>
</div>
如果第一个面板位于焦点上,则调用路由面板更新方法。在这里,我想加载一个模板。我不想在 html 文件中制作 if 标签。
U 可以在父div 上使用 ng-bind-html="yourVariable" 并在 contoller 中更改您的变量并且 mbe 您需要在更改值后使用 $sce.trustAsHtml(yourVariable(并包括"$sce"作为控制器的依赖关系
您可以在需要时使用 ng-include
指令与 ng-if
结合使用来包含模板。
<div ng-if="fish ==='salmon'" ng-include="'whatever.html'"></div>
<div ng-if="fish ==='perch'" ng-include="'whatever2.html'"></div>
好的。我自己得到了解决方案。
我使用$templateRequest,然后将html转换为实际的DOM节点。在此之前,我使用 empty(( 清除旧数据:
this.routePanelUpdate = function (element) {
routePanelCounter += 1
if(routePanelCounter == 10){
element.empty();
$templateRequest(templateURL).then(function (html) {
var template = angular.element(html);
element.append(template);
});
}
};
如果有人有更好的解决方案,请告诉我。
相关文章:
- 通过js在新选项卡中有条件地打开url
- 我已经创建了一个jquery转盘,并使用if条件来运行和停止转盘
- jQuery-有条件地附加HTML
- 根据某些条件在视图之间切换
- 如何做到这一点,使代码在不传递条件后执行函数
- 有条件更新d3.js力图中节点的最佳方法
- javascript中的布尔条件
- Woocommerce产品选项有条件
- 基于两个条件退出While循环
- 有条件地在选项标记中应用布尔属性
- 使用javascript以HTML形式验证条件
- 聚合物0.5.5:核心列表中的条件模板和/或模板动态参考
- 有条件的取决于样式
- 仅将特定元素添加到 html 正文中,具体取决于 javascript 条件
- 取决于下拉选择的多个条件表达式
- 一个指令中的模板取决于条件
- 条件javascript公式取决于checkbox是否被选中
- If条件,取决于类是哪个文本
- 改变css属性取决于如果条件与右键按
- If条件取决于css