简体   繁体   English

单击内部单元格模板不会触发控制器中的功能

[英]ng-click inside cell template does not trigger function in controller

I have created a plunker here: http://plnkr.co/edit/zGqouwzxguef13lx48iP?p=preview 我在这里创建了一个plunker: http ://plnkr.co/edit/zGqouwzxguef13lx48iP?p = preview

When I click in a cell of the ui-grid in the day view then nothing happens. 当我在日视图中单击ui-grid的单元格时,没有任何反应。 What I expected is that the test function is executed and an alert is shown with text 'test' but that is not the case. 我所期望的是执行测试功能并显示带有文本'test'的警报,但事实并非如此。

What is going on wrong here? 这里出了什么问题?

Thats the html cell template of the ui-grid 3.0 latest release: 这是ui-grid 3.0最新版本的html单元模板:

HTML HTML

<div ng-click="test()" ng-switch="row.entity[row.grid.columns.indexOf(col)].isPeriod">

    <div ng-switch-when="true">
        <tabset>
            <tab>
                <tab-heading>
                    <i class="glyphicon glyphicon-book"></i>
                </tab-heading>period id:
                {{ row.entity[row.grid.columns.indexOf(col)].id}}
            </tab>
            <tab select="alertMe()">
                <tab-heading>
                    <i class="glyphicon glyphicon-bell"></i>
                </tab-heading>
                {{row.entity[row.grid.columns.indexOf(col)].content}}
            </tab>

        </tabset>      <!-- PeriodTemplate -->
    </div>
    <div ng-switch-when="false">
       <div>Hello empty template</div>
    </div>      <!-- EmptyPeriodTemplate -->
</div>

CONTROLLER: 控制器:

'use strict';
angular.module('projectplanner').controller('DateplannerDayController', function ($scope, $state) {


    var columnHeaderDates = ['col1','col2','col3','col4','col5','col6','col7']
    $scope.columns = createColumnHeaders(columnHeaderDates);

var data = [{isPeriod: true, id: 10, rowNumber: 1},{isPeriod: false, id: 11, rowNumber: 2}]

  $scope.test = function()
  {
    alert('test');
  };

    $scope.gridOptions = {
        rowHeight: 200,
        data: data,
        enableSorting: false,
        enableColumnMenu: false,
        columnDefs: $scope.columns,
        onRegisterApi: function (gridApi) {
            $scope.gridApi = gridApi;
            $scope.gridApi.core.addRowHeaderColumn(
                { name: 'rowHeaderCol',
                    displayName: '',
                    width: 100,
                    cellTemplate: '<div>row header template</div>'
                });
        }
    };

    function createColumnHeaders(columnHeaders) {
        var columnDefinitions = [];
        for (var i = 0; i < columnHeaders.length; i++) {
            var column = {
                name: columnHeaders[i],
                cellTemplate: 'lessonplanner.day.celltemplate.html',
                field: i + 'x'
            }
            columnDefinitions.push(column);
        }
        return columnDefinitions;
    }
});

This page kept popping up in my searches and I managed to miss the solution in the comments. 这个页面在我的搜索中不断涌现,我在评论中设法错过了解决方案。 To summarize, you likely need to use externalScopes. 总而言之,您可能需要使用externalScopes。 See Angular ui-grid events in cell header not firing and http://ui-grid.info/docs/#/tutorial/305_appScope 请参阅单元头中未发射的角度ui-grid事件http://ui-grid.info/docs/#/tutorial/305_appScope

如果您使用Controller-As语法,请尝试:

ng-click= "grid.appScope.<controllerAliasName>.myFunction(row)"

It is because the ui-grid has its own controller and it doesn't know about the outside controller. 这是因为ui-grid有自己的控制器,它不知道外部控制器。

To facilitate, do the following.. 1. First assign the controller to the ui-grid. 为方便起见,请执行以下操作:1。首先将控制器分配给ui-grid。

var vm = this;
this.$scope.usersGridOptions = {
 appScopeProvider: vm,
 ....
}

Now, once you assign the controller to the grid, you can invoke the function like this.. 现在,一旦将控制器分配给网格,就可以像这样调用函数。

"<a type=\"button\"  href=\"\" ng-click=\"function(row.entity)\"> {{ row.entity.text}} </a>" 

In ui-grid, cell-template, because of cell has its own scope, 在ui-grid,cell-template中,由于cell有自己的范围,

1) do not use onclick='', instead should use ng-click=''

2) ng-click='your_function()' will not work, should use, grid.appScope.your_function()

3) when pass parameter in function(), do not use function({{xxx}}), see sample below

cellTemplate: '<div class="ui-grid-cell-contents" title="TOOLTIP"><a href=""  ng-click="grid.appScope.watering_modal(grid.appScope.editLink_tree_id(grid, row),grid.appScope.editLink_site_no(grid, row),grid.appScope.editLink_crm(grid, row), grid.appScope.editLink_package_no(grid, row) )">{{grid.appScope.editLink_tree_id(grid, row)}}</a></div>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM