3

这是指令

aomApp.directive('aomAlert', function ($rootScope,$compile) {
return {
    restrict:'EA',
    transclude:true,
    replace:true,
    scope: {type: '=', msgCollection: '=', close: '&'},
    controller: function ($scope, $element, $attrs,$compile) {
        $scope.show = false;

        $scope.$watch('msgCollection', function(selectedPlan) {
            $scope.show = ($scope.msgCollection.length > 0);
        });                     
    },
    template: 
        "<div class='alert' ng-class='type && \"alert-\" + type' ng-show='show'>" +
        "   <button ng-show='closeable' type='button' class='close' ng-click='show = false;close();'>&times;</button>" +
        "   <ul>" +  
        "       <div ng-repeat='msg in msgCollection'><li><div ng-bind-html-unsafe=msg></div></li></div>"+
        "   <ul>" +  
        "</div>",
    link: function($scope, $element, $attrs) {
        $scope.closeable = "close" in $attrs;

    }


};

});

在控制器中,我将链接放入 msg var

msg = msg.replace("[", "<a href='javascript:return false' ng-click='alert()'>");
                msg = msg.replace("]", "</a>");

但是 ng-click 没有被触发 有人吗?

4

1 回答 1

1

将某些内容放入 html-bind-unsafe 不会编译它。您必须告诉元素使用范围进行编译。这是关于 $compile 的文档:http: //docs.angularjs.org/api/ng .$compile

编辑好的,您不需要在评论中的回复中使用 $compile 。有两种方法可以做到这一点,一种是你告诉 clickme 在作用域上调用 $parent.clickme:

scope.clickme = function(){
    scope.$parent.clickme();
};

小提琴:http: //jsfiddle.net/a4ang/3/

另一种是您将 clickme 作为属性传递给指令:

scope: { 
    data: '=',
    clickme: '='
},

html

<toggle-buttons data="data" clickme="clickme"></toggle-buttons>

更新小提琴:http: //jsfiddle.net/a4ang/4/

于 2013-06-19T15:00:54.403 回答