【发布时间】:2015-12-17 12:19:29
【问题描述】:
我在Jsfiddle 进行了 angularjs 示例的演示。 我只是在创建一个 angularjs 指令,它将显示一个背景颜色为红色的 div,其中包含作为模板的数据绑定的静态文本。
var objApp = angular.module("myApp", []);
objApp.controller("myAppController",
function($scope) {
$scope.name2="temp";
});
objApp.directive("myAppDirective",
function(){
return {
template : "<div style='background-color:red'>"+
"Hello {{name}}"+
"<div ng-transclude></div>"+
"</div>",
scope : true,
transclude : true,
controller : function($scope) {
$scope.name = "tempController";
},
compile : function(tElem, attrs, transcludeFn) {
return function(scope, iElem, attrs, transcludeFn) {
console.log(iElem.html());
//iElem.html("<div ng-show='false'>" +
// iElem.html() +
// "</div>");
}
}
});
当注释下一行时,每个绑定都可以正常工作。
iElem.html("<div ng-show='false'>" + iElem.html() + "</div>")
但是一旦评论被删除,绑定就会被破坏。为什么会这样?我必须在 iElem 上设置的新 html 上使用 $compile 吗?当这一行被评论时,它是否已经编译?我一直在寻找答案,但没有一个能帮助我。
【问题讨论】:
标签: javascript angularjs angularjs-directive