Invoking a scope function within a user interface grid hyperlink

As I delve into creating tables with UI-Grid, I encountered an issue where I need to trigger a scope function when the content of a cell is clicked. Essentially, I want an alert to pop up when a cell is clicked on. Below is my JavaScript code snippet:

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

}]);

Furthermore, here's the structure of my web page:

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

Despite following this setup, the functionality does not work as intended and troubleshooting remains ongoing.

Answer №1

If you need to invoke the $scope level function from a template, you must utilize grid.appScope. In your situation, it would look like this: grid.appScope.test()

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

See an example here http://plnkr.co/edit/KyxhAXQVYPfnZPcNh9Ce?p=preview

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing variables from reinitializing in express.js

Within my app.js file, I invoke a search function that communicates with an LDAP server. Upon doing so, the server sends back a cookie which is essential for my subsequent queries. My intention was to save this cookie as an app.local variable in my app.j ...

Is there a distinction between "muted" and "volume = 0.0"? Event listeners handle them in separate ways

I am a newcomer to coding and have encountered an issue that has me stumped. I am working on developing a small media player intended for use with the athletes I coach. The concept is to require them to listen to my commentary while watching game footage. ...

How can I make AngularJS $state open a link in a new tab?

Is there a way in AngularJS to implement an "open link in new tab" function using the $state.go method? It would be very helpful if we could do something like this: $state.go('routeHere', { parameter1 : "parameter" }, { reload : true ...

Summon wicket from the non-wicket-component entity

I am currently utilizing js-lib to transform text into a rich-editor, complete with options such as "bold", "italic" and more. The RichEditor also includes an UploadImage button, for which I am able to modify the call-back function: function startUploadi ...

AngularJS consistently executes preflight requests despite lacking the application/json content type

Currently, I am facing a dilemma as I have extensively researched both Stack Overflow and the documentation. According to my findings, Angular's $http function will only initiate an OPTION request if the content-type is set to application/json. Below ...

What is the method for aligning an object perpendicular to a surface in Three.js?

Check out this relevant codepen: http://codepen.io/OpherV/pen/yNebep In the game I am working on, there is a unique alien tree model. To create spikes on each face of the tree, I am generating pyramids using CylinderGeometry with 4 faces and positioning ...

Uncovering the characteristics of a GeoJSON data layer within Google Maps V3

Is there a way to access the properties of the data layer itself when loading a geoJSON file into a Google Map? I understand how to access the individual properties like posts_here, but I'm interested in obtaining the properties for the layer as a wh ...

Avoiding the capturing of events on $( document ).mousemove

Each time the browser detects a $( document ).mousemove event, my function is invoked. The performance is smooth with an empty page, but once I introduce a div element and hover over it, the function gets executed twice: first on the document and then agai ...

Creating a fresh discord.js v11.3 channel

I've been working on a ticket bot, but I'm encountering issues with my !new command. Can someone please assist me with this problem? I've been referring to Discord's documentation for guidance. Below is the code snippet I'm using: ...

Avoid the situation where the prop overrides the existing data

I am facing a challenge with vue.js as a beginner. I have an array filled with objects that I send through props to my router-view. Within one of my router-view components, I am using this array in multiple functions by referencing it with 'this.data ...

insert a gap between two elements in the identical line

Is there a way to create spacing between two text fields in the same row? I have tried using margins, paddings, and display flex in the css file but haven't been successful. import "./styles.css"; import TextField from "@material-ui/cor ...

Leverage the retrieved JSON data from the database within an HTML template using the Play

Currently, I am facing a challenge with implementing a login feature in my project. I am struggling to figure out how to showcase the JSON string that is returned on the webpage. Below is a snippet of my code: Security.java: public Person webLogin(Perso ...

Adding additional filters to an already existing filter function while querying Azure Mobile Services

In my Angular Web App, I am utilizing the Azure Mobile Services library for JavaScript. The documentation states that there are two methods to filter returned data: passing a JSON object or using a filter function for more complex filtering. Since I need t ...

Learn the process of transforming JSON data into an array with the structure provided

Struggling to fetch data from a Firebase Realtime database and pass it into Google Spreadsheets as JSON objects. Any help would be greatly appreciated. Here is the structure of my Firebase Realtime database: { "4rjF1iwudEXEevtzEoGwDrEtEpI3": { " ...

What is the best way to update all APIs in a React component whenever a new action is initiated?

import AppTextInput from '@/components/form-fields/AppTextInput'; import Iconify from '@/components/iconify/Iconify'; import AppButton from '@/components/templates/AppButton'; import AppDataGrid from '@/components/templat ...

Storing the result of browser.executeScript in a variable with Protractor

I'm facing a challenge in storing the value of browser.executeScript within a local variable in my it block. In some cases, it returns null which is not desired. I've experimented with different approaches but haven't found a reliable solut ...

Can an object type be partially defined?

Here is a similar scenario: function(param1, { knownParam1, ...opts }) To better describe param1 and knownParam1, perhaps something like this could work: type Param2 = { knownParam1: string, ...otherParams: any } type Parameters = { param1: str ...

Tips for adjusting the duration of a background in jQuery

jQuery(document).ready(function(){ jQuery('div').css('background-color','#ffffff').delay(12000).css('background-color','#000000'); }); Hello everyone! I'm having some trouble with this code. I am ...

Exploring navigation within a React application

After attempting to develop a react native application and finding it too difficult for my needs, I decided to switch to React. As a beginner in web programming, I recall learning about the concept of 'navigation' in react native, including vario ...

Dilemma arises from conflicting javascript codes

Currently, I am developing a web application where the main page features a timeline that needs to update its content automatically. To achieve this, I am utilizing the setTimeOut function of JQuery to refresh the timeline every x seconds. In addition, th ...