Using ng-transclude directive in AngularJS for input values

My goal is to develop a directive that includes the ng-transclude value in the input field's value attribute within an HTML template:

The directive I have created:

module.directive('editInput', function(){
    return {
        restrict: 'E',
        scope: {
            value: '='
        },
        transclude: true,
        template: '<p ng-show="value == false" ng-transclude></p>' +
        '<input ng-show="value == true" placeholder="" value="" ng-transclude/>'
    }
});

I am looking for a solution that adds ng-transclude value to the value attribute of the input element.

Here is the template in use:

<edit-input value="isEditModeActive">{{person.name}}</edit-input>

Currently, the generated HTML output looks like this:

<input ng-show="value == true" placeholder="" value="" ng-transclude="" class="">
<span class="ng-binding">Name</span></input>

However, the desired HTML output should be:

<input ng-show="value == true" placeholder="" value="Name">

Answer №1

code.js:

angular.module('app', [])
.controller('ctrl', function($scope) {
  $scope.user = {};
  $scope.user.name = 'Alice';
})
.directive('editField', function(){
    return {
        restrict: 'E',
        scope: {
            data: '=',
            editValue: '@'
        },
        transclude: true,
        template: 
        '<p ng-show="data == false" ng-transclude></p>' +
        '<input ng-show="data == true" placeholder="" value="{{editValue}}" />'
    }
});

home.html:

<!doctype html>
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8>
    <title>Home Page</title>
</head>
<body ng-controller="ctrl">
    <script src= "angular.js"></script>

    <script src= "code.js"></script>

    <edit-field data="true" edit-value="{{user.name}}">{{user.name}}</edit-field>
    {{user.name}}
</body>
</html>

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

Nextjs attempting to access local storage without proper initialization

I'm currently working on a feature where I have a sidebar with two buttons, contact and profile. When a user clicks on either of them, the URL updates to: /?section=clickedItem, allowing the user to save the state and return to that specific page upon ...

Tips for creating a custom filter to sift through an array of objects by comparing their values with those in a separate object

We have successfully implemented an advanced search feature in AngularJS, which includes 15 input fields. Upon page load, the result set is retrieved from the database in JSON format and we are required to perform filtering on the client side. Each input ...

Click on the checkbox to activate it using JavaScript

I'm trying to use JavaScript to toggle the checkbox status when clicking a button. The first button is supposed to uncheck the box and it's working correctly: function clear() { document.getElementById("check").checked = ""; } However, I al ...

Loading screen for specific content within the current WordPress theme

I am trying to display a preloader only in the 'content' div, but it ends up hiding the entire page. The structure of the site is as follows: Title Menu Content (where I want the preloader) Footer I'm having trouble figuring out where exa ...

The viewport width in NextJS does not extend across the entire screen on mobile devices

I'm currently tackling a challenge with my NextJS Website project. It's the first time this issue has arisen for me. Typically, I set the body width to 100% or 100vw and everything works smoothly. However, upon switching to a mobile device, I not ...

Activate Bootstrap modal using an anchor tag that links to a valid external URL as a fallback option

To ensure accessibility for users who may have javascript disabled, we follow a company standard of developing our web pages accordingly. Our target demographic still includes those familiar with VCRs blinking 12:00. One particular challenge involves a te ...

What is the best approach to modify the model value when a controller function is triggered?

This is the controller: onSelectRow : function(id){ setTimeout(function () {$scope.getSelectedRow(); }, 0); },}; $scope.getSelectedRow = function(){ var grid = $("#patientgrid"); var rowKey = grid.jqGrid('getGridPara ...

Webpack fails to handle CSS background images

I'm having trouble with my Webpack configuration as it's not processing CSS images set in the background property: background: url('./hero.jpg') no-repeat right; This is resulting in an error message that reads: ERROR in ./src/app/comp ...

Is it detrimental to my search engine ranking if I utilize CSS display:none?

Imagine having valuable content that is initially hidden with CSS, and then using javascript to reveal it only when the user clicks on certain links. Even users without javascript enabled can access this content by following the links to a new page where i ...

How can I mirror just one side of a texture in Three.js / WebGL?

I am attempting to create a kaleidoscopic effect using only one side, but I have a large number of Points and would like the effect to be achieved within the shader. If there is a Threejs trick that can mirror half of the texture or the Points object, that ...

Is it possible to animate share buttons using Framer Motion but staggering siblings?

I have a Share Icon that looks like: I'm looking to display the first 5 icons, with the 5th icon being the share icon. The remaining icons should appear below the share icon and expand to the right when someone taps or hovers over it (either tap or h ...

JavaScript - creating mathematical expressions without the need for operator overloading

In my JavaScript project, I am utilizing three.js and attempting to determine if a vector remains within a designated area as shown below: var THRESHOLD = 10; if (!is_in_rect(camera.position - forward * THRESHOLD)) // do something where camera.positi ...

Version 4.6.4 of TypeScript is flagging the code as invalid

How can I fix this Typescript problem? const userInformation: { email: string; id: string; _token: string; _tokenExpirationDate: string; } = JSON.parse(localStorage.getItem('userData')); https://i.sstatic.net/xMh9P.pn ...

Find the matching ID and consolidate all the information under one single ID

I am looking to merge objects with the same title and consolidate their data into a single object. new = [{ "data": [{ "displayName": "Peter pvt ltd", "name": "Peter Zon"}], "title&quo ...

Tips for implementing an HTML modal with AngularJS binding for a pop up effect

As a beginner in AngularJS, I am facing a challenge. I have an HTML page that I want to display as a pop-up in another HTML page (both pages have content loaded from controllers). Using the router works fine for moving between pages, but now I want the s ...

Extract the content from the division and set it as the image source

Looking for a way to retrieve the content from a div and insert that into the 'src' parameter of an image. Working on a project where JSON is used to load translation files, preventing me from loading images directly, but I want to at least load ...

The ng-model remains empty even after the $parent has been defined

I've encountered an issue where assigning ng-model to a text area and an input does not update the models. It is mentioned that this could be due to using ng-if causing a different scope, and I should use $parent, but even that solution is not working ...

When a menu item is clicked, apply a class and change the display property to

I currently have an HTML menu with a submenu structured as follows: <li id="item-3" class="menu-item has-children-3"> <a href="#" class="sf-with-ul"><span>Auto</span></a ...

How can I efficiently make a dropdown menu with search functionality instead of using datalist?

I am currently working on creating a custom searchable input field that makes backend calls. Instead of using datalist, I want to design a unique UI with ul and li items. While I have successfully implemented the search functionality, I am facing a glitc ...

The www file is only loaded by Node Inspector when the preload setting is turned off

Whenever I start node-inspector The node-inspector browser window successfully loads all the files. https://i.sstatic.net/Ctx2K.png However, when I use node-inspector --preload=false Only my bin/www file is loaded on the node-inspector window. http ...