Calling the scope function in a UI mesh element

I am working on creating tables using a UI-Grid. I want to call a scope function when I click on the contents of a cell. In this case, when a click is clicked, a warning should appear. Here is my javascript file.

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

app.controller('MainCtrl', ['$scope', '$http', '$interval', '$q', function ($scope, $http, $interval, $q) {
    $scope.gridOptions = {};

    $scope.gridOptions.columnDefs = [{name: 'ID', field: 'id', cellTemplate: '<a ng-href="#" ng-click="test()">{{row.entity.id}}</a>'}];

    $scope.test = function() {
        window.alert("Alert");
    }

}]);

And my webpage:

<!doctype html>
<html ng-app="app" class="ng-scope">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular-animate.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.js"></script>
    <script src="app.js"></script>
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">

    <style type="text/css">
    .grid {
        width: auto;
        height: 500px;
        margin-left: 50px;
        margin-right: 50px;
        margin-top: 50px;
        margin-bottom: 50px;
    }
    </style>

  </head>
  <body>

<div ng-controller="MainCtrl">
  <div id="grid0" ui-grid="gridOptions" ui-grid-exporter ui-grid-selection class="grid"></div>

</div>

  </body>
</html>

This does not work, and I cannot understand why.

+4
source share
1 answer

To call a $ scope level function from a template, you have to use grid.appScope. In your case it will begrid.appScope.test()

cellTemplate: '<a ng-href="#" ng-click="grid.appScope.test()">{{row.entity.Locked}}</a>'

An example here is http://plnkr.co/edit/KyxhAXQVYPfnZPcNh9Ce?p=preview

+5
source

Source: https://habr.com/ru/post/1599425/


All Articles