Angular directives do not support the compilation of HTML code within them

angular.module("vtApp.directives").
directive('multirangeslider', function ($compile) {
    return {
        restrict: 'E',
        replace: true,
        link: function (scope, element, attrs) {
            var variances, values, options, template, compile;
            scope.variances = eval (attrs.variances);
            scope.values = scope.$eval(attrs.values);
            var htmlText = '<div id="'+attrs.id+'"><table class="CRC"><tbody><tr>';
            for (var i=0; i<scope.values.length; i++) {
                htmlText += '<td id="val' + i + '" style="width:' + scope.values[i] + '%;"> ' + scope.variances[i] + ': <strong>{{ranges[' + i + ']}}%</strong></td>';
            }
            htmlText += '</tr></tbody></table></div>';

            template = angular.element(htmlText);
            $compile(template)(scope);
            element.replaceWith(htmlText);

        }
    }
});

and within the view I am using:

<multirangeslider values="ranges" variances="['Master', 'Template A', 'Template B']"></multirangeslider>

The html content is not being compiled. You can see an example of this issue in this JsFiddle

Answer â„–1

compiledTemplate represents the compiled element. The initial html string is stored in originalHTML. Replace the line

element.replaceWith(originalHTML);
with
element.replaceWith(compiledTemplate);
.

Answer â„–2

I made updates to your fiddle and it is now functioning properly.

http://jsfiddle.net/timactive/SXSXf/2/

var myApp = angular.module('myApp',[]);

//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {

    $scope.name = 'Superhero';
     $scope.ranges = [33, 33, 34];
}

myApp.
directive('multirangeslider', function ($compile) {
    return {
        restrict: 'E',
        scope :{ranges:'='},
        replace: true,
        link: function (scope, element, attrs) {
            var variances, values, options, template, compile;
            scope.variances = eval(attrs.variances);

           // scope.values = scope.$eval(attrs.values);
            var htmlText = '<div id="' + attrs.id + '"><table width="100%" cellspacing="0" cellpadding="0" border="1"><tbody><tr>';
            for (var i = 0; i < scope.ranges.length; i++) {
                console.log(scope.ranges[i]+" " + scope.variances[i]);

                htmlText += '<td id="val' + i + '" style="width:' + scope.ranges[i] + '%;"> ' + scope.variances[i] + ': <strong>{{ranges[' + i + ']}}%</strong></td>';
            }
            htmlText += '</tr></tbody></table></div>';
            console.log(htmlText);

            template = angular.element($compile(htmlText)(scope));
            //$compile(template)(scope);
            element.replaceWith(template);


        }
    }
});

html :

<div ng-controller="MyCtrl">
    {{name}}
    {{ranges | json}}
  <multirangeslider ranges="ranges" variances="['Master', 'Template A', 'Template B']"></multirangeslider>
    <br />
    range1 : <input type="text" ng-model="ranges[0]"/>
    range2 : <input type="text" ng-model="ranges[1]"/>
    range3 : <input type="text" ng-model="ranges[2]"/>

</div>

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

``The Art of Handling REST API with Express and Mongoose

Running Express on my application, I have a delete route set up as shown below: router.route('/lists/:id') .delete(function(req, res){ Entry.remove({ _id: req.params.id }, function(err, list){ if(err) ...

Discord.JS Guild Member Cache Responses that are not recognized as valid

My automated messaging bot has been running smoothly for the past 6-8 months, but recently it encountered a strange issue with a specific user. Upon checking the cache of the Discord server it operates on, I noticed that it only returned two members - myse ...

Using Angular's ng-href directive in conjunction with the Split method

I am encountering an issue with a recursive variable that has a specific value path = <d:URL>http://www.google.com, Google</d:URL> My goal is to bind this path into an Angular component. I have attempted to use the following method to bind th ...

Switch to light mode upon clicking the toggle?

On the top left corner of this page, there's a toggle for "Light/Dark mode". Clicking on 'Light mode' smoothly transitions to dark mode without any issues. However, once it's in Dark mode and you try to switch back to 'light mode&a ...

Translating .NET webclient request into JavaScript Syntax

I have a piece of .NET code that I need help converting into Javascript/jQuery. I'm thinking it might involve using AJAX, but I'm not sure how to go about it. Can someone here assist me with this? public  string  SendSMS(string  recipient,  ...

What is the best way to access specific information about the top card in a Trello list?

Is there a way for me to retrieve the name, label, and due date of the top cards in a Trello list on my board and then send it through Discord using my bot? I would greatly appreciate any guidance on how to achieve this. ...

Revise the modal window

I need help with switching between two modal windows for registration and login. How can I make the modal window change when the "Sign Up" button is clicked? Here is the link to my project: https://jsfiddle.net/Alienwave/0kqj7tr1/4/ Vue.component('s ...

If there is only one remaining option after applying the angular filter, automatically choose that option

Currently, I have a scenario where there are two select boxes. The selection in one box acts as a filter for the other box. In some cases, this filter results in only one option left in the list. I am exploring options to automatically select that lone opt ...

Different ways to showcase a value from the CSS file on the console using console.log

In this guide, you can learn how to create a custom directive in Angular by following this tutorial: Custom Directive Tutorial. The directive should function as intended. Still, I want to see the color value set in the CSS file displayed on the console us ...

Using Javascript to Manuever an Element via Arrow Inputs

Currently, I am in the process of developing a Tetris game with a rotating Tetris piece controlled by pressing the space bar. My next objective is to enable the movement of objects to the left and right using the arrow keys. After exploring various simila ...

Creating a reusable service for making REST API calls with $http

I am in need of a versatile service that I can call from my controller with the ability to pass options dynamically. For Instance. var app = angular.module('common', []); app.factory('APIService', ['$http', function($http){ ...

I am seeking a way to conceal list elements according to a specified string input using JavaScript

Looking at the list items inside this div: <div class="container"> <h2>Vanishing Act Game</h2> <div> <li class="list-group-item go" id="item">Door#1</li> <li class="list-group-item go" id="it ...

How can we enable Material UI tooltip based on certain conditions?

In my React component utilizing Material UI, I have the code snippet below: const MyButton = ({ warningText }) => ( <Tooltip title={warningText}> <Button>Do action</Button> </Tooltip> ) At present, an empty tool ...

What could be causing my jQuery to fail when the page first loads?

My script only seems to work when the input changes, but I want it to start working as soon as the page loads and then continue to update when the input changes. Can anyone help me figure out what's wrong with my script? <script src="http://co ...

Vue is struggling to load the component files

Lately, I've been encountering an error during webpack build that is causing some issues. ERROR There were 3 errors encountered while compiling The following relative modules could not be found: * ./components/component1.vue in ./resources/assets/ ...

Prevent duplicate components from interacting with one another in Angular

My Tabs component has its own variables and functions, and it works perfectly. However, I encountered an issue when trying to place multiple tab components on the same page. Whenever I change the value of one tab, it also affects the other tab component. ...

Guide on efficiently utilizing ui-select within the ng-repeat directive

My ui-select element is wrapped in an ng-repeat directive, causing a scope-related issue. This results in a few problems: Once a value is selected in the first select, the second select is automatically populated with the same value. The placeholder ...

Transferring information to a partial view using a jQuery click event

In my Index view, there is a list of links each with an ID. My goal is to have a jQueryUI dialog box open and display the ID when one of these links is clicked. Currently, I am attempting to use a partial view for the content of the dialog box in order to ...

"Discover the process of utilizing jQuery to trigger an action when scrolling past a

I'm currently working with this code snippet: #main { max-width: 500px; height: 900px; margin: auto; background: green } .menu1 { height: 30px; background: red } .menu2 { display: none; } <div id="main"> <div class="menu1"& ...

The Perfect Scrollbar feature is not functioning properly in conjunction with the accordion menu

I am having some trouble with implementing the Perfect Scrollbar alongside an accordion slider menu. The scrollbar is not working as expected, and you can view the fiddle here. On initial page load, the scrollbar only appears for the first sub-menu aft ...