Ways to generate a customized template using the directive attribute parameter

I have developed a new directive and I am looking to incorporate a dynamic template using the attribute wm.data.typeName.

wm.data.typeName = "<span>html code</span>" 

<fill-choose model-input="wm.data.modelInput" text="wm.data.typeName"></fill-choose>

The key component here is the directive fillChoose.

(function() {
  'use strict';

  angular
    .module('learn')
    .directive('fillChoose', fillChoose);

  /** @ngInject */
  function fillChoose($showdown) {
    var directive = {
      restrict: 'AE',
      template: function(elem, attr) {
        //return $showdown.makeHtml(fill.modelInput);
        return '<div>'+ attr.modelInput +'</div>';
      },
      scope: {
        modelInput: '=',
        text: '='
      },
      controller: FillChooseController,
      controllerAs: 'fill',
      bindToController: true
    };

    return directive;

    /** @ngInject */
    function FillChooseController($scope) {
      var vm = this;
    }
  }

})();

However, I am facing an issue where the template output is

<div>wm.data.modelInput</div>
.

Is there a way to ensure that the template renders as

<div><span>html code</span></div>
instead?

Answer №1

 link: function(scope, elem, attr) {
    //return $showdown.makeHtml(fill.modelInput);
    elem.html($compile('<div>{{fill.modelInput}}</div>')(scope));
  }

In this particular scenario, a straightforward solution would be to use:

template:'<div>{{fill.modelInput}}</div>';

However, the reason I included the $compile function is because you may need a more advanced approach depending on your specific requirements.

Answer №2

To display it, you must output the content

<custom-select data-input="{{ info.data.input }}" label="info.data.name"></custom-select>

However, keep in mind that this will only function one time, as the template function is only called once during directive initialization (when Angular compiles the template). This is why it is crucial to pass it through the directive's scope

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

A guide to programmatically downloading a file with JavaScript on Internet Explorer

I'm currently facing a challenge with my web page. There is a button that, when clicked, should generate a CSV file (by converting from JSON) for the user to download. I've implemented the logic based on this jsfiddle, and it works perfectly in C ...

Guide to using a Selector in a mixin in VUE

I'm attempting to create a Mixin that will help me calculate the offsetWidth of an element. Here is my Mixin: export const boxWidth = (selector) => ({ mounted() { selector.addEventListener( 'resize', this.setBoxWidth ); ...

Tips on using CSS to hide elements on a webpage with display:none

<div class="span9"> <span class="disabled">&lt;&lt; previous</span><span class="current numbers">1</span> <span class="numbers"><a href="/index/page:2">2</a></span> <span class="num ...

Failed jQuery AJAX request to database with no returned information

I'm really confused about where the issue lies :S The button triggers a function that passes the parameter "sex" and initiates an ajax call to ajax.php, where I execute a MySQL query to retrieve the results and populate different input boxes. When I ...

Improving code efficiency for checkboxes in upcoming TypeScript versions

Is it possible to create a single checkbox component and dynamically pass different values to it without repeating code? I have set up these checkboxes to help me check the maximum and minimum values of some API data. const[checkValMax, setCheckValMax]= u ...

Guide on organizing json object by various elements with distinct sorting patterns

Below is a JSON Object that needs to be sorted based on different conditions: var data1 = [ {id: "1", name: "b", lastname: "y", marks: "10"}, {id: "1", name: "a", lastname: "x", marks: "20"}, {id: "2", name: "a", lastname: "x", marks: "30" ...

Is it feasible to transmit telemetry through a Web API when utilizing ApplicationInsights-JS from a client with no internet connectivity?

My Angular application is running on clients without internet access. As a result, no telemetry is being sent to Azure. :( I am wondering if there is a way to configure ApplicationInsights-JS to call my .Net Core WebApi, which can then forward the inform ...

Switching templates in AngularJS

We have a unique challenge from our client who wants a responsive website, but desires to make significant content changes that may push the limits of bootstrap. Although bootstrap allows for showing/hiding blocks and adjusting their positions with offset ...

The successful execution of one promise determines the outcome of the $q.all promise

I am currently dealing with a situation where I have three nested promises that I need to refactor into a single $q.all call. The current structure of the code looks like this: ds.saveData(data).then(function (result1){ someOtherVar = result1.Id; ...

Having trouble grasping the problem with the connection

While I have worked with this type of binding (using knockout.js) in the past without any issues, a new problem has come up today. Specifically: I have a complex view model that consists of "parts" based on a certain process parameter. Although the entire ...

Efficient ways to temporarily store form data in React JS

When filling out a registration form and clicking on the terms and conditions link, the page redirects to that content. Upon returning to the registration page, all fields are empty and need to be filled in again from scratch. I am looking for a way to ha ...

Execute the function numerous times that is associated with an asynchronous function in JavaScript

I am currently working on two functions: one is asynchronous as it fetches data from a CSV file, and the other function renders a list of cities. The CSV file contains information about shops located in various cities. My goal is to display a list of cit ...

Problem with Java class in GWT JsInterop

Having some trouble with JsInterop while wrapping up a piece of JavaScript code. The JavaScript code looks like this: com = { gwidgets: {} }; com.gwidgets.Spring = function () { this.name = "hello"; }; com.gwidgets.Spring.prototype.getName = ...

Is it better to use regexp.test or string.replace first in my code?

When looking to replace certain parts of a string, is it better to use the replace method directly or first check if there is a match and then perform the replacement? var r1 = /"\+((:?[\w\.]+)(:?(:?\()(:?.*?)(:?\))|$){0,1})\ ...

Unexpected behavior with ng-show binding

I am currently working on implementing a toggle feature in my form. The idea is that when I click one button, it should display a section with the corresponding name, and hide the other sections. However, I am facing an issue related to scope. When I do no ...

Insight on the process of submitting interactive forms

I'm facing a challenge that I can't seem to figure out It's a form structured in HTML like this: <button (click)="addform()">add form</button> <div class="conten-form"> <div class="MyForm" ...

Having a solid grasp of React and Material UI, as well as familiarity with the concept of props and

For my react application, I utilize Material UI (@mui/material). However, I've come to realize that there might be some nuances in how the components interact with each other. For instance, if I nest a MenuItem inside a Box or a Link inside a Typograp ...

Comparing the syntax of JSON to the switch statement in JavaScript

I recently came across a fascinating post on discussing an innovative approach to utilizing switch statements in JavaScript. Below, I've included a code snippet that demonstrates this alternative method. However, I'm puzzled as to why the alter ...

What methods can a server use to identify if JQuery Ajax's "withCredentials: true" option was utilized for CORS requests?

Currently, I am working on integrating CORS (Cross-origin resource sharing) into a framework. I understand that when an XMLHttpRequest request is made using Jquery's ajax(...) with the withCredentials property set to true, the server must specificall ...

How can I convert a string to an integer in Node.js/JavaScript in terms of cardinality?

Imagine a scenario where a user can input any string such as "1st", "2nd", "third", "fourth", "fifth", "9999th", etc. The goal is to assign an integer value to each of these strings: "1st" -> 0 "2nd" -> 1 "third" -> 2 "fourth" -> 3 "fifth" -&g ...