How can we use the same name for the ng-model as the variable itself instead of just pointing to the variable?

Below is a directive I am working with:

.directive('gdInputField', function() {
    return {
        //only applies to elements, not attributes.
        restrict: 'E',
        template: "<label for='{{name}}'>{{label}}</label><input name='{{key}}' id='{{name}}' type='{{type}}' ng-required='{{required}}' /><p>{{entry.1949113882}}</p>",
        scope: {
                label: '@label',
                name: '@name',
                key: "@key",
                required: "@required",
                type: "@type"
        },
    };
})

I want the value set with @key to be the model name for the input field. If I use ng-model='key', it shows the @key string as the content of ng-model.

This seems to be the outcome:

$scope={
     someting: @key
}

What I actually need is:

$scope={
   @key: '';
}

If the user inputs something in the field, @key should update accordingly.

Additionally, what is the current name of the model binding or how can I determine that?

Answer №1

Revise your code in the following manner

app.directive('gdInputField', function () {
    return {
        //Only applies to elements, not attributes.
        restrict: 'E',
        template: "<label for='{{name}}'>{{label}}</label><input name='{{inputName}}' ng-model='key' id='{{name}}' type='{{type}}' ng-required='{{required}}' /><p>{{key}}</p>",
        link: function (scope, elem, attrs) {
            scope.inputName = attrs.key;
        },
        scope: {
            label: '@label',
            name: '@name',
            key: "=",
            required: "@required",
            type: "@type"
        },
    };
});

The crucial aspect is that the key must be bidirectional in order to serve as a model.

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

JavaScript code to generate an array of arrays containing prime factors:

Is there a method to generate an array of arrays representing prime factors and then filter it to retain only the necessary factors to generate numbers in a given set? For example, for numbers 1-16, the array set would look like this: [ [2,2,2,2], [3,3], ...

Deleting the stylesheet exclusively within the confines of the React application window

Here is an image that will help illustrate the issue: https://i.stack.imgur.com/VA7fw.png If you want to check out the code sandbox for this problem, you can visit: https://codesandbox.io/s/annoying-stylesheet-2gpejc?file=/public/index.html I am current ...

How come my React application is not fetching the latest version of the state variables?

Currently, I am actively engaged in developing a software application that retrieves an SVG file and transforms it into a collection of React components with customized interactive features. The obstacle I am encountering pertains to the onClick function ...

Guidelines for passing multiple Javascript values to a C# action method

Assuming I have a piece of JavaScript code similar to the one shown in the image. I populate an array with multiple values and then pass this array to an action method in a controller. However, in the action method, I receive this array as an 'object& ...

My attempts to display the filtered outcomes are unsuccessful

Consider this scenario where I have an array of objects (pros). These pros offer various services, each represented by an object with a serviceName and serviceType. const pros = [ { name: "Tony", email: "<a href="/cdn-cgi/l/email-protection" c ...

Ways to verify the timeframe between two specific dates

Having two distinctive arrays: accomodation: [ { id: 1, name: "Senator Hotel Fnideq", address: "Route de Ceuta, 93100 Fnidek, Morocco", checkin: "September 1", fullCheckinDate: "2021-09-01", ...

``I'm having trouble invoking a separate method within [displayWith] in Angular Material's autocomplete feature

Converting an address object into a string format for displaying the complete address in autocomplete is what I am trying to achieve. Below is the code snippet: address.component.html <mat-form-field class="address-autocomplete"> <input type=" ...

Ext 4.1 - Accessing a class instance using Ext.define()

In my code, I have a class with a method defined like this: Ext.define('MY.class.Manager', { .... .... .... manageStuff: function(){ } } Initially, the manageStuff function was only needed in one place and everything worke ...

Ways to retrieve an object from a separate function in JavaScript

I am struggling to fetch data from an array and store it in an object. However, every time I try, I end up with either an empty object or a Promise { <pending> } shown in the logs. I have attempted to use a global variable to store the data and acc ...

The WPF WebBrowser struggles to display the generated HTML from a website built with Angular JS

I'm currently developing a WPF application and I need to extract the HTML content of a website that utilizes Angular JS technology. Here is my current approach: First, I have created a WPF Web Browser control: private WebBrowser webBrowser; Next, I ...

how to implement a collapsed row feature in an angular table row

My table contains collapsed rows with additional 2nd-level information, but not all rows have this data. Is there a way to create a controller that will display a 2nd level collapse row only if the corresponding JSON script includes 2nd level data? ...

Tips for choosing multiple values from a dropdown menu in Bootstrap CSS version 3

I'm looking to implement a way to select multiple values from a dropdown menu without using the CTRL key. Currently, I am utilizing Bootstrap CSS for styling. Here is the code for my dropdown: <select multiple class="dropdown-menu"> ...

PhantomJS encountered an error during the comparison of identical objects, resulting in the test

I'm currently developing an Angular.js application that utilizes Bootstrap (and jQuery). For testing, I am using Karma Jasmine with PhantomJS. One of the tests I wrote executes a method from my controller and compares two identical objects. it(&apos ...

Is there a way to utilize ajax to submit a form and upload a file to a spring controller?

I have a form with four fields: file, name, type (as a string), and taskInstanceId. <form> <table id="documentDetailsTable"> <tr> <td>Document Type: </td> <td><select id="documentType" ...

Is there a way to add a background image similar to that?

Take a peek at the following site: . Notice how, as you scroll down, the background image appears to be fixed. It's almost like one of the pages acts as a window allowing you to see the background image. How can I achieve this cool effect? ...

Tips for defining a variable beyond the confines of the controller in AngularJS

I need to create a variable that can be accessed by two different controllers. One controller is responsible for posting values while the other retrieves values from a database. To achieve this, I have implemented a factory method that returns a timestamp. ...

Loading CSS files conditionally in Angular2's index.html

Currently, my index.html page features a dark theme: <base href="/"> <html> <head> <title>XXX</title> </head> <body> <link rel="stylesheet" type="text/css" href="assets/dark_room.css"> <my-app ...

Error message: "Root resolution error encountered". Need assistance in granting watchman access?

After setting up a react-native starter project, I was prompted to allow watchman access to my system's files. Unfortunately, I declined and now whenever I try to run the folder, I encounter the following error: Error: unable to resolve root /Users/ck ...

Troubleshooting the Loading Issue in Java Spring Boot with AngularJS

Having trouble loading a simple page to display all messages? I'm using Angular for the front-end and a Spring Boot app for the back-end. When I attempt to load the page (localhost:8080/messages), it appears blank and prompts me to download JSON with ...

SyntaxError in ExpressJS: Encountered an unexpected token "C"

I'm having trouble saving my string to a comma-separated array. When I attempt to use the JSON.parse method, I encounter an error while sending a post request and trying to save a record: SyntaxError: Unexpected token c at Object.parse (native) ...