angularjs指令中的handsontable - 渲染具有ng-click的锚点

Lan*_*rry 3 angularjs handsontable angularjs-directive

所以我使用Handsontable渲染网格.(是的,我没有使用ngHandsontable.我从那开始,但遇到了问题,所以我只是从angularjs指令渲染一个Handsontable.)

我想要一个列来保存锚标记.

我希望anchor标签具有angularjs ng-click指令.

所有内容都正确呈现,但不会调用ng-click .

这是我的例子.

var APP = angular.module('APP', ['controllers']);

angular.module('controllers',[])
.controller('testController', function ($scope) {
    $scope.doNgClick = function() {
        alert('ng-click');  
        // console.log('ng-click');  
    };
    $scope.simple = [
        {
            test: "<a href='javascript:void(0);' ng-click='doNgClick()'>Test</a>"
            // test: "<a ng-click='doNgClick()'>Test</a>"
        }
    ];
});

APP.directive('htable',function($compile) {
    var directive = {};
    directive.restrict = 'A';
    directive.scope = {
        data : '='
    };
    directive.link = function(scope,element,attrs) {
        var container = $(element);
        // var safeHtmlRenderer = function (instance, td, row, col, prop, value, cellProperties) {
        //     var escaped = Handsontable.helper.stringify(value);
        //     td.innerHTML = escaped;
        //     return td;
        // };        
        var settings = {
            data: scope.data,
            readOnly: true,
            colHeaders: ['Link'],
            columns: [
                {   
                    data: "test",
                    renderer: "html", 
                    // renderer: safeHtmlRenderer,
                    readyOnly: true
                }
            ]
        };
        var hot = new Handsontable( container[0], settings );
        hot.render();
        // console.log(element.html());
        // $compile(element.contents())(scope);
    };//--end of link function
    return directive;
});
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="//handsontable.com/dist/handsontable.full.css">
  </head>

  <body>

    <div ng-app="APP">
        <div ng-controller="testController">
            <div htable data="simple"></div>
        </div
    </div>

    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>
    
    <script src="//handsontable.com/dist/handsontable.full.js"></script>

  </body>

</html>
Run Code Online (Sandbox Code Playgroud)

Lan*_*rry 5

经过多次阅读和挖掘,这是我自己的答案.

//-- With help from the following:
//--
//-- http://stackoverflow.com/questions/18364208/dynamic-binding-of-ng-click
//-- http://weblogs.asp.net/dwahlin/creating-custom-angularjs-directives-part-3-isolate-scope-and-function-parameters
//--

var APP = angular.module('APP', ['controllers']);

angular.module('controllers',[])
.controller('testController', function ($scope) {
    $scope.click = function(msg) {
        console.log('ctrl_doNgClick: ng-click: msg: '+msg);  
    };
    $scope.simple = [
        {
            test: "<a href='javascript:void(0);' ng-click='dir_ctrl_click(\"blah1,blah1\")'>Test 1</a>"
        },
        {
            test: "<a href='javascript:void(0);' ng-click='doClick(\"blah2,blah2\")'>Test 2</a>"
        },
        {
            test: "<a href='javascript:void(0);' ng-click='doClick(\"blah3,blah3\")'>Test 3</a>"
        }
    ];
});

APP.directive('htable',function($compile) {
    var directive = {};
    directive.restrict = 'A';
    directive.scope = {
        data  : '=',
        click : '&'
    };
    directive.controller = function($scope) {
        $scope.dir_ctrl_click = function( msg ) {
            console.log('controller: dir_ctrl_click: click via the directive controller method');
            $scope.click()(msg);
        };
    };
    directive.link = function(scope,element,attrs) {
        var container = $(element);
        scope.doClick = function(msg) {
            console.log('link: doClick: click via the directive link method');
            scope.click()(msg);
        };
        var linkHtmlRenderer = function (instance, td, row, col, prop, value, cellProperties) {
            //-- here is the magic that works
            //-- the method, in ng-click, must either be defined here in the link method or in the controller method (the example data contains both)
            var el = angular.element(td);
            el.html($compile(value)(scope));
            return el;
        };        
        var settings = {
            data: scope.data,
            readOnly: true,
            colHeaders: ['Link'],
            columns: [
                {   
                    data      : "test",
                    renderer  : linkHtmlRenderer,
                    readyOnly : true
                }
            ]
        };
        var hot = new Handsontable( container[0], settings );
        // hot.render();
    };//--end of link function
    return directive;
});
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="http://handsontable.com/dist/handsontable.full.css">
  </head>

  <body>

    <div ng-app="APP">
        <div ng-controller="testController">
            <div htable data="simple" click="click"></div>
        </div
    </div>

    <script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
    
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.2.24/angular.min.js"></script>
    
    <script src="http://handsontable.com/dist/handsontable.full.js"></script>

    
  </body>

</html>
Run Code Online (Sandbox Code Playgroud)