AngularJS:ng-click无法在ui-grid cellTemplate中工作

时间:2017-05-16 09:09:50

标签: javascript html angularjs

我再次走向角色问题。

我的HTML中有一个网格,这只是一行。

我正在对控制器进行复制。

app.controller('PanelController', function ($scope,  $compile, uiGridConstants){

    var actionTemplate = '<div class="ui-grid-cell-contents"><img class="addNotes" src="images/button/detail.gif" ng-click="dettaglio(row.entity)" /></div>';

    $scope.dettaglio = function(ritornoSearch, inspect) {
        console.log("make it function");
    };

    columnDefs: [
        { field: 'azioni', enableFiltering: false, width: 85, enableSorting: false, enableColumnMenu: false, cellTemplate: actionTemplate, displayName: 'Azioni'},
        { field: 'codeSubInstaller', headerCellClass: $scope.highlightFilteredHeader },
        { field: 'nomeSubInstaller', headerCellClass: $scope.highlightFilteredHeader },
        { field: 'cognSubInstaller', headerCellClass: $scope.highlightFilteredHeader },
        { field: 'codeFiscaleSubInstaller', headerCellClass: $scope.highlightFilteredHeader },
        { field: 'descStato' , headerCellClass: $scope.highlightFilteredHeader }
    ]
};

问题是:当我在浏览器中打开它时,图像不会显示为可点击。如果我试图点击它,它甚至不能提供console.log。

我错过了什么?

1 个答案:

答案 0 :(得分:1)

Just do it like its documented in http://ui-grid.info/docs/#/tutorial/305_appScope and compare this runnable plnkr demo with your solution.

$scope.grid.appScope is available in all templates that the grid uses. In a template, you access the scope using grid.appScope property


In that way you need to change your template into the right syntax: ng-click="grid.appScope.dettaglio(row)":

 var actionTemplate = '<div class="ui-grid-cell-contents"><img class="addNotes" src="images/button/detail.gif" ng-click="grid.appScope.dettaglio(row)" /></div>';

AngularJS application example with ui-grid:

var app = angular.module('app', ['ngTouch', 'ui.grid']);

app.controller('MainCtrl', ['$scope', '$log', '$http', function ($scope, $log, $http) {

    $scope.dettaglio = function (row) {
        console.log(row);
        alert('inside');
    };

    $scope.gridOptions = {};

    $scope.gridOptions.columnDefs = [
        {name: 'name'},
        {name: 'gender'}, {
            name: 'ShowScope',
            cellTemplate: '<button class="btn primary" ng-click="grid.appScope.dettaglio(row)">Click Me</button>'
        }
    ];

    $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/100.json').success(function (data) {
        $scope.gridOptions.data = data;
    });

}]);