我想问下 。。 比如 : 点击按钮 生成 一段html代码。现在是 我想在这段html 加上directive指令 ,并且 加个 click事件,但是没效果。 click事件没效果。要怎么做啊?
app.directive("sadsdas",function(){
return {
restrict : "A",
link : function(scope,element,attrs){
element.on("click",function () {
console.log("sdsd")
});
}
}
})app.directive("itemPopover",function($timeout){
return {
restrict : "A",
link : function(scope,element,attrs){
element.popover();
var createText = "<div class='hover' sadsdas style='position:absolute;top:50%;left:125px;width:400px;z-index:9999999;font-size:26px;color:#fff;text-align:center;'>请输入文本</div>";
var con = angular.element(".app-content");
element.on("click",function(){
if(attrs.tag == "createText"){
console.log(con)
$timeout(function(){
con.append(createText);
});
}
});
}
}
});现在点击 sadsdas 的div 没输出 console.log("sdsd")
app.directive("sadsdas",function(){
return {
restrict : "A",
link : function(scope,element,attrs){
element.on("click",function () {
console.log("sdsd")
});
}
}
})app.directive("itemPopover",function($timeout){
return {
restrict : "A",
link : function(scope,element,attrs){
element.popover();
var createText = "<div class='hover' sadsdas style='position:absolute;top:50%;left:125px;width:400px;z-index:9999999;font-size:26px;color:#fff;text-align:center;'>请输入文本</div>";
var con = angular.element(".app-content");
element.on("click",function(){
if(attrs.tag == "createText"){
console.log(con)
$timeout(function(){
con.append(createText);
});
}
});
}
}
});现在点击 sadsdas 的div 没输出 console.log("sdsd")
我是在link函数中以setAttribute方法,将指令以字符串的形式放到元素中,然后用你这招,防止编译后会重新链接。所以在用着招时加个判断,用setAttribute方法设置个标志位,以防止循环,当然我这句话不知道是不是有点多余,上段link函数中的代码。 if(attrs.name){
selectEle.setAttribute("name",attrs.name);
} if(attrs.check){
selectEle.setAttribute('ng-required','districts.length>0');
} if(!attrs.cok){
//设置编译成功标识,然后编译
selectEle.setAttribute('cok','true');
$compile(elem.contents())(scope);
}