Angular 指令事件未在编译函数中触发

Angular directive event not triggered in compile function

我尝试了很多但没有成功,在编译函数中绑定的情况下如何触发ng-click?

function() {
        return {
            restrict: 'EA',
            compile: function(element, attr) {
                return function(scope, element, attr) {
                    element.html('<div ng-click="save()"></div>');
                }
            },
            controller : function($scope){
                $scope.save = function(){
                    console.log('save');
                }
            }
        };
    }
])

使用 link 和 $compile 生成您需要的结果:

return {
            restrict: 'EA',
            link: function (scope, element) {
                element.html('<div  ng-click="save()">save</div>');
                $compile(element.contents())(scope);
            },
            controller: function ($scope) {
                $scope.save = function () {
                    console.log('save');
                }
            }
        }