Tips for creating ui-sref links by using a mix of translate and ng-bind-html

I'm having trouble figuring out how to create a functional ui-sref link from a translated string.

Using angular 1.4.9 along with angular translate 2.9.0

Below is the relevant code snippet

<div ng-bind-html="$scope.getTranslatedText(someObject)"></div>

controller {
   function(value) {
       this.$translate.isPostCompilingEnabled(); // Returns true
       return this.$translate.instant("taskNames."+value.parameters['messageId'], value.parameters);
   }
}

The translation contains the following string.

taskInstructions{
    someMessageId: "Here is some text <a ui-sref=\"goSomewhere\">Some more text</a>"
}

The translation displays the text correctly, and without the ng-bind-html the ui-sref link is visible. However, once the ng-bind-html is added, the ui-sref disappears. I attempted to use compile, but it did not work as expected. I also tried using $sce.trustAsHtml to display the ui-sref in the HTML output, but it did not create a functional link. It seems like I am overlooking a step somewhere, unsure of how to proceed in order for the ui-sref to work as intended.

Any advice on best practices to resolve this issue?

Answer №1

Instead of using the translate feature, we can leverage the $sce and $state services:

HTML

<section class="adb-layout-row" ng-repeat="post in vm.posts">
  <div class="adb-layout-default">
    <h5><a ng-click="vm.open(post)">{{ post.title }}</a></h5>
    <div ng-bind-html="post.html"></div>
  </div>
</section>

JS

$q.when(getPosts()).then(function(data) {
  vm.posts =  data.posts.map(function(post){
      return {
        title: post.title,
        html: $sce.trustAsHtml(post.description),
        url: post.url
      };
    });
});

angular.extend(vm, {
  open: function(post) {
    $state.go(post.url);
  }
});

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

Gain access to PowerBI reports effortlessly without the need to input any credentials

Seeking guidance on calling Power BI reports from an ASP.NET C# web application while passing credentials, specifically without utilizing Azure AD. Access has been granted to certain users in the Power BI Service workspace with view permissions. We aim t ...

Leveraging Server Response Codes Across Different Domains Using JavaScript

Before anything else, I must acknowledge the cross-domain issues that may arise with JavaScript. The fact that I am seeing both 404 and 200 response codes in my console has made me reconsider this possibility. The scenario... I am currently developing a w ...

Is there a way to apply an event function after adding elements through append?

When I click the button, a div is appended inside the body. However, I am trying to make it so that when I click on this newly appended div, an alert message pops up. I have tried implementing this with a highlighted area, but have been unsuccessful. How c ...

Conceal the element if the offspring is devoid of content

I want to implement a feature where a div is hidden when its child element is empty. To achieve this, I aim to assign the class .no-content to the div if it contains no elements. Below is my existing code with spaces: <div class="ee-posts-list&quo ...

Encountering errors preventing the utilization of helpers in fancybox2-rails gem

I've been struggling to implement fancybox2 in my RoR app. I've attempted using the gem and manually adding the files to my assets directory, but I'm having issues with the helpers. Currently, the thumb images generated by the helper are no ...

Generate an array from a string where certain words are substituted with objects featuring the correct formatting styles

Can someone help me with this code challenge? I have a specific string: const str = 'The world is full of various colors. For example: red, green, blue.'; In my dictionary, I have words and their corresponding styles. const styles = { RED: ...

Tips on updating time zone settings in a browser using React JS or JavaScript

I am currently facing a requirement where I need to adjust the application's time based on an environment variable. The entire application should operate using the timezone specified in the configuration file. For instance, if the designated timezone ...

What is the method to ensure that the Node REPL solely displays the result?

Is there a way to execute a script in Electron that only logs the output value without displaying all the code? I am utilizing xterm.js and node-pty for this project. For instance, consider the following sample code: // Add your code here function multi ...

My HTML files are not getting bundled by Webpack

Despite including dependencies for HTML loaders in my Angular 2 application, webpack is not bundling my HTML files along with the rest of the files it generates. I have tried using both "html-loader" and "html-webpack-plugin," but to no avail. My webpack ...

The resizing issue persists with Angularjs charts

I have recently developed a small web application using AngularJS and I have implemented two charts from the AngularJS library - a bar chart and a pie chart. Although both charts are rendering correctly, they are not resizing properly as the display size c ...

Using Vue.js, pull information from a database to populate input fields using a datalist feature

I'm currently working on a project utilizing Laravel 8 and Vue 3, and I have a Student Component. Within this component, there is a datalist that allows me to search for existing students. When I select a student from the list, I want the correspondin ...

Ways to circumvent route handling in Angular UI Router

I'm currently developing a web application where most of the functionality is implemented as an AngularJS single-page application. However, there are also some static content pages that are served traditionally. The navigation within the SPA utilizes ...

Header Overflow Error Encountered in Node.js GET Request

While attempting to programmatically submit a form through Google forms using a GET request, I encountered the error message Parse Error: Header overflow. The debug code output is as follows: REQUEST { uri: 'https://docs.google.com/forms/d/e/9dSLQ ...

Sending confidential data from the view to the controller without relying on form submission

I am looking for a way to pass a hidden field from a view to a controller. Inside index.chtml <div id="root"> ................ @Html.Hidden("HProjectTypeId", "somevalue") </div> The above code snippet is not enclosed within any form tags ...

How to pass the Node environment to layout.jade in Express without explicitly specifying the route

Passing parameters to Jade files seems like a piece of cake: app.use('/myroute', function (req, res) { res.render('myview', {somevar: 'Testing!'}); }); But, I have my layout.jade file that is automatically read and rendere ...

Unusual occurrences when making several ajax requests to a single URL

I've encountered a peculiar scenario while working on a CherryPy server, and I'm seeking assistance in understanding the intricacies behind it. Here's the content of server.py: import cherrypy import os import threading class Root(object): ...

React is throwing an error message stating that setCount is not a valid function

Getting an error saying setCount is not a function. I am new to this, please help. import React, { memo, useState } from "react"; export const Container = memo(function Container() { const { count, setCount } = useState(0); return ( ...

Maintain authentication state in React using express-session

Struggling to maintain API login session in my React e-commerce app. Initially logged in successfully, but facing a challenge upon page refresh as the state resets and I appear as not logged in on the client-side. However, attempting to log in again trigge ...

Scroll horizontally on a webpage using drag with JavaScript

I have been working on a unique horizontal website design that allows users to scroll horizontally by dragging the screen. I managed to successfully implement the horizontal scrolling feature, but I am facing difficulties in adding the horizontal drag-to-s ...

Looking to preserve the "ALL" selection in a JavaScript-CSS filter?

On the front page, there are approximately 16 posts displayed along with a filter featuring 4 drop-down menus showcasing post categories. I have assigned category names as classes to each post div and am currently using javascript to hide them. The code s ...