我想问下 。。 比如  : 点击按钮  生成 一段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")

解决方案 »

  1.   

    得重新编译$compile(element.contents())(scope),但我还没成功。兄弟你的搞好了没?主要我的没有用指令,而是用的数组然后通过ng-repeat迭代出来,效果是实现了,但表单验证却没有反应了。坑啊
      

  2.   

    需要在click绑定的方法最后面加上$scope.$apply();因为事件的触发是”异步“的,angular不知道什么时候完成,因此你需要手动同步双向绑定
      

  3.   


    我是在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);
                    }