Handcrafted in angularjs directive - rendering an anchor that has ng-click

So I am using Handsontable to render the mesh. (Yes, I am NOT using ngHandsontable. I started with this, but ran into problems, and so I just went with the Handontable from angularjs directive.)

I want one column to contain an anchor tag.

I want the anchor tag to have the angularjs ng-click directive.

Everything displays correctly, but ng-click does not call .

Here's my example.

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;
});
      

<!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 codeHide result


+3


source to share


1 answer


After much reading and digging my own answer.



//-- 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;
});
      

<!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 codeHide result


+5


source







All Articles