小编fre*_*y83的帖子

Angular ng-click无法使用$ compile

我有类似于以下代码的代码来触发clickAngular应用程序中的事件.为什么事件不会触发?

var app = angular.module("myApp", [])

app.directive('myTop',function($compile) {
return {
    restrict: 'E',
    template: '<div></div>',
    replace: true,
    link: function (scope, element) {
        var childElement = '<button ng-click="clickFunc()">CLICK</button>';
        element.append(childElement);
        $compile(childElement)(scope);

        scope.clickFunc = function () {
            alert('Hello, world!');
        };
    }
}
})
Run Code Online (Sandbox Code Playgroud)

javascript angularjs angularjs-directive angularjs-ng-click

4
推荐指数
1
解决办法
4239
查看次数