Binding Angular's ng-model to the element's name property

Is there a way to dynamically bind ng-model to a scope property with the same name as the element?

For example, in this code snippet:

<input name="myinputname" type="text" ng-model="?" />

Is there a method to set the ng-model value to resolve to "myinputname" without manually entering it?

Answer №1

To optimize performance, consider incorporating the ng-model directly from the server side.

If implementing it on the client-side is necessary, you can create a personalized directive that automatically adds the ng-model during compilation:

app.directive('myModel', function($compile) {
  return {
    restrict: 'A',
    replace: false,
    priority: 1000,
    terminal: true, 
    link: function (scope, element, attrs) {
      attrs.$set('ngModel', attrs.name); 
      attrs.$set('myModel', null); 

      $compile(element)(scope); 
    }
  };
});

Usage example:

<input type="text" name="myinputname" my-model />

Upon compilation, it will transform into:

<input type="text" name="myinputname" ng-model="myinputname" />

Explore this concept further: http://plnkr.co/edit/hBQQMDTr6cYtHzFvoAaQ?p=preview

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

Encountered an error in React where the declaration file for a module could not be located

New to Typescript and trying to incorporate a splitter into my project. I'm utilizing SplitPane from "react-split-pane/lib/SplitPane" and Pane from "react-split-pane/lib/Pane" in my Typescript project, but encountering an error: Could not find a de ...

Executing Controller Actions within a JavaScript Timer

Presenting my latest timer: var eventDate = new Date(Date.parse(new Date) + 3600); function countdown() { var elapsed = Date.parse(eventDate) - Date.parse(new Date()); var seconds = Math.floor((elaps ...

The custom modal does not seem to be compatible with the CSS3 animation library,

I managed to create my own Full page modal successfully, but now I want to enhance it with an animation. I am attempting to use animate.css to add animation effects such as zoomIn or zoomOut, but it's not working as expected. Here is the link to my J ...

Combining numerous base64 decoded PDF files into a single PDF document with the help of ReactJS

I have a scenario where I receive multiple responses in the form of base64 encoded PDFs, which I need to decode and merge into one PDF file. Below is the code snippet for reference: var pdf_base1 = "data:application/pdf;base64,JVBERi0xLjQKJfbk/N8KMSAwIG9 ...

jquery button returned to its default state

Jquery Form Field Generator |S.No|Name|Button1|Button2| When the first button is clicked, the S.No label and name label should become editable like a textbox. It works perfectly, but if I click the second button, the field becomes editable as expected, ...

The lack of intellisense in VS Code for Node.js poses a significant challenge for developers

I am facing a problem with IntelliSense in Visual Studio Code. Despite installing multiple extensions, it is not working as expected. The IntelliSense feature only shows functions and variables that have already been used in my code file. Below is the li ...

My webpage is experiencing issues with function calls not functioning as expected

I have created a select menu that is integrated with the Google Font API. To demonstrate how it works, I have set up a working version on JSBIN which you can view here. However, when I tried to replicate the code in an HTML page, I encountered some issues ...

Update the HTML form action to use the Fetch API to communicate with the webserver

I successfully managed to store files on my computer by utilizing the HTML form action attribute and then processing this request on my Express webserver. However, when I attempted to switch to using an event listener on the submit button of the form ins ...

How can I trigger the second animation for an object that is constantly moving within a specific range in the first animation?

I am looking to create a simulation of rising and falling sea levels. Currently, when I click the button, the level rises from its starting point to the top, but I am unsure how to achieve this effect in my code. Below is the code I have written so far, an ...

What is the best way to utilize MUI breakpoints for displaying images based on different screen sizes?

I need help displaying an image based on the screen size using MUI breakpoints. I'm struggling to understand how to implement this with MUI. Can someone assist me with the breakpoints? interface EmptyStateProps { title: string; description: string ...

Searching through a populated field in MongoDB

If I want to search for a specific book based on the reviews field, how can I do that? The reviews field contains an array of objects, and one of the keys in each object is the description. If my search query matches the description of any review, then I w ...

What is the process for transferring an image from a cloud function request to the vision API?

Just set up a Google Cloud Function with an HTTP endpoint. I want to pass an image to this endpoint and have it sent all the way to the Vision API (which is already set up) and then receive the response back. So, in essence: image in POST request -> C ...

Generating a random number within a JSON field: A guide

I need help generating a random number for the Corelation Id field in my JSON. I tried using the function ${__Random(1, 10000)} but it's not working. Any suggestions on how to achieve this? "LoginRequest": { "Header": { ...

When using HttpContext.Current.GetOwinContext().Authentication.Challenge(), the ADFS page failed to open as expected

I have developed a single page MVC application that utilizes AngularJS to interact with APIs from my ASP.NET MVC application, including handling the login functionality. I am looking to implement single sign-on (SSO) in my application. Prior to redirectin ...

Incorporate object keys into an array using JavaScript

Query: I'm working on a JavaScript project and I have an array that looks like this: [6.7, 8, 7, 8.6]. I need to transform this array into an array of objects with named properties: [{y: 6.7} , {y: 8}, {y: 7}, {y: 8.6}]. Can someone guide me on how to ...

What is the best way to adjust the padding on the left and right of the Bootstrap navbar logo and menu items?

I am currently facing an issue with the alignment of the logo and menu on my website. They are currently aligned to the far edges of the screen, and I would like them to remain a set percentage distance away from the extremes, rather than an absolute dista ...

How can I configure Angular.js in Webstorm?

I'm having trouble setting up a node.js and angular.js project in webstorm as I keep encountering this error message: /usr/bin/node app/src/app.js /home/dac/WebstormProjects/web-plugin/app/src/app.js:1 ion (exports, require, module, __filename, __dir ...

Tips for initiating a browser file download using JavaScript, ensuring that the download is only carried out if the web service responds with

For a project I am working on, I need to create JavaScript code that will be triggered by clicking on an <a> element. This code will then make a GET request to a web service that I am currently in the process of coding. Once the service returns a fil ...

Displaying numbers individually with commas in a Django template

I am facing an issue where the number being typed into a text field does not have commas separating the thousands For example, if the user types 500000, it displays as 500000 instead of 500,000 This issue is present in the models section of the code: so_ ...

Is it possible to eradicate arrow functions in Angular CLI?

I am facing a challenge in running my code on IE11 due to issues with arrow functions. I need to find a way to eliminate them from the build and replace them with function() {}. Even though I have removed them from main.js, they are still present in the v ...