What is the best method for embedding <ng-click> in a string within Endpoint.java and then returning this string to the client side?

I'm currently developing a messaging feature within a Chat application that facilitates communication between logged in users via the Web Socket protocol. In order to enhance user interaction, I am looking to incorporate a reply option into each message. I attempted to nest a link tagged with reply within the message, along with an angular tag using ng-click to trigger a modal window for replying (as shown below).

The ng-click content consists of a function that takes the message ID as input for replying.

However, my issue arises when clicking on reply - the reply modal appears but the designated function is not executed!

Below is the snippet from ChatEndPoint.java:

String m="<div>
             <div>" + 
                 _content 
          + "</div>  &nbsp; 
             <a href='#' data-toggle='modal' data-target='#replyModal'
              ng-click='storemsgid("+_msgid+")'><small>reply</small></a>
          </div> ";

Key Points:

  • The variable _content holds a String value and _msgid contains an Integer value.
  • _content is displayed in the chat console.
  • A function called storemsgid(id) exists in the HTML page, which is functional and not causing issues.
  • The new message will be added to the chat console (contained within a div tag) with its own ng-controller, including the storemsgid(id) function.

I would greatly appreciate any assistance or guidance on this matter.

Thank you

Answer №1

Implement the ng-bind-html directive to dynamically insert HTML content from a controller:

  <body ng-app="myApp" ng-controller="myVm as vm">
    <h1>Creating Click Handlers with AngularJS</h1>
    <p>{{vm.rawMsg}}</p>

    <div ng-bind-html="vm.msg"></div>

  </body>

JavaScript:

app.controller("myVm", function($sce) {
  var vm = this;
  vm.rawMsg = '<button onclick="angular.element(this).scope().vm.alert(99)">Alert 99</button>'
  vm.msg = $sce.trustAsHtml(vm.rawMsg);

  vm.alert = (x) => alert(x);
})

The ng-bind-html directive doesn't support compiling AngularJS directives like ng-click. Instead, utilize the onclick attribute in conjunction with angular.element for invoking scope functions:

<button onclick="angular.element(this).scope().vm.alert(99)">
   Alert 99
</button>

Check out the DEMO on PLNKR.

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

What type of JavaScript scope is accessible by an anchor tag's href attribute?

My current query involves using an <a> tag to invoke a JavaScript function: <a href="javascript:doSomething();">link</a> In which scope does this JS function need to be defined in order to be reachable? Is declaring it globally necessar ...

What could be causing my Angular JS application to malfunction?

Presenting myFirstAngularApp.html <html ng-app="store"><!-- The "ng-app" attribute initializes an Angular app. Everything inside this tag is considered part of the Angular app due to ng-app --> <head> <link rel="stylesheet" type= ...

Create a React MUI component that allows menu items to become sticky when selected

Is there a way to make the mui MenuItem stay sticky to Select while scrolling down? To see the issue in action, check out this codesandbox example https://codesandbox.io/s/quirky-knuth-5hr2dg?file=/Demo.tsx Simply click on the select and start scrolling ...

How to Extract Nested JSON Data in Java using Regular Expressions

Exploring the world of regex, I am on a mission to convert nested JSON into strings. Check out some sample examples below: My desired output string format is as follows: {"mainId":"12345","binaries":[{"subId":"123456bd","splitAll":true},{"subId":"123456c ...

Angular-datatables has a scroller function that allows for easy navigation within the content of

I've been scouring the web for hours, but I can't seem to find a solution. Can someone please assist me in resolving this issue? Issue: I integrated angular-datatables within an md-tab using Angular material. Everything was running smoothly unti ...

Concealing a div class using javascript

Currently, I am working on a feature that involves using checkboxes to hide and show a specific div class. Although the code is successful in hiding the div, I am encountering difficulty when attempting to show it. I suspect there may be an error somewher ...

Maintain the visibility of the jQuery dropdown menu even when navigating to a different page within the

I am experiencing an issue with my dropdown menu. Whenever I click on a "dropdown link", it opens another page on my website, but the menu closes in the process. I want to ensure that the menu stays open and highlights the clicked "dropdown link" in bold. ...

Encountering Error 1 in Cordova File Transfer with Laravel Framework

I'm currently developing an Ionic angular 1 application where I need to upload an image to my laravel api server. However, I keep encountering error code 1 (File not found?) along with a laravel PHP error message as the response body. I've tried ...

All you need to know about the AngularJS Toggle functionality

I am a beginner in AngularJS and I have adapted jQuery code into a directive. When I click on one of the numbers, it toggles an action. Can someone please assist me with this? Here is my code on jsfiddle angular.module('app', []) .directive ...

Appium - Exploring the Wonders of Android Hybrid Apps

Upon running the test script, I encountered a console error stating 'driver null'. Script: capabilities.setCapability("appium-version", "1.4.0"); System.out.println("<<< Detects android device / emulator >>>"); capabilities.se ...

I am having trouble with the prime number finder in my JavaScript program. It seems to not work for certain values. What could be

I am in the process of developing a code to identify prime numbers less than n. However, I encountered an issue where the code mistakenly flags 33 and 35 as prime numbers. I am puzzled by this unexpected outcome. Here is the code that I have been working o ...

When the mouse button is released or when an event listener is

I've been pondering a question that has yet to be fully answered. When I implement this technique to catch a mouse up event: <div onmouseup="/*Script to be executed*/"></div> Is it more efficient than this newer approach: <div id=" ...

"Encountering an issue with Next.js where the Redux

Hey there, I'm working on a simple project using Nextjs. I need to access the state of my Redux store but I'm encountering an error when trying to use store.getState, it's throwing an error saying getState is undefined. Additionally, I have ...

Show the button's value on the text box using JavaScript

When using bootstrap, I encountered an issue where the value of a button would display in a textbox upon clicking it, but then quickly disappear. This unexpected behavior left the textbox empty prematurely. <input type="submit" value="5000t "class="btn ...

Storing data retrieved from an asynchronous call in AngularJS to a variable

Here is the code where I am attempting to utilize promise to store data from an asynchronous call in a variable, but it's not functioning as expected. I am relatively new to promises and after some research, I learned that promises can be helpful in s ...

Experiencing an "ENOTFOUND" error after attempting to make a large volume of API calls, possibly in the range of 100,000, to maps.google

I have a requirement to send a large number of requests to https://maps.googleapis.com/maps/api/place/queryautocomplete/json. Currently, I am fetching lists of strings from multiple files and sending requests to the mentioned API. When I test with 100 str ...

Determine if two instances are within the same week, where each week starts on Friday and ends on Thursday, using moment.js

Currently, I'm in the process of developing a Discord bot using node.js and discord.js. One of the features allows users to vote through a command, but I want to restrict them to voting only once per week. The challenge lies in the fact that in this ...

Include an image in a Material-UI card without specifying a file path

I'm looking to include an image in a CardMedia component, but the image is currently stored as a base64 string like this: iVBORw0KGgoAAAANSUhEUgAAASwAAACoCAMAAABg99fBOxrQANHibkS1KoplYAFHgUGz85g6Ggnn2ysaMXSlccHislKRm7uI10eB9piJYuPjE3epRveUuwrTNidn9ad2 ...

Converting a single 10GB zip file into five separate 2GB files with the help of NodeJS

var fs = require('fs'); var archiver = require('archiver'); var output = fs.createWriteStream('./test.zip'); var archive = archiver('zip', { gzip: true, zlib: { level: 9 } // Sets the compression level. }); ...

The forked library fails to generate a dist folder within the node-modules directory

Having an issue with a Vue.js plugin from GitHub. Here's the link: https://github.com/antoniandre/vue-cal I forked it and made some changes, but when I try to install the node_modules folder, the dist folder is missing. Any idea what could be causing ...