Exploring the versatility of input types in Angular.js dynamic forms

Attempting to develop an Angular.js app that generates dynamic form inputs from a JSON object.

To begin with, there is a JSON object (referred to as fields):

[
  {"field_id":209,"form_id":1,"name":"firstname","label":"First Name","type":"text"},
  {"field_id":210,"form_id":1,"name":"lastname","label":"Last Name","type":"text"},
  {"field_id":211,"form_id":1,"name":"email","label":"Email","type":"text"},
  {"field_id":212,"form_id":1,"name":"picture","label":"Picture","type":"file"},
  {"field_id":213,"form_id":1,"name":"address","label":"Address","type":"file"},
  {"field_id":214,"form_id":1,"name":"password","label":"Password","type":"password"},
  {"field_id":215,"form_id":1,"name":"","label":"","type":"submit"}
]

The key type in the object represents the input type for a form. Displayed below:

<p ng-repeat="field in fields">
  {{field.name}} : <input type="{{field.type}}" value="{{record.data[field.name]}}" />
</p>

This method works well for submit, text, password, checkbox, and radio fields. However, when the type is file, it defaults the input type to text.

When I substitute {{field.name}} with {{field.type}} for text, it demonstrates "file" correctly.

If I manually modify

<input type="{{field.type}}"...
to <input type="file"..., it will show a file input accurately.

Why am I unable to dynamically assign an input type as a file?

Answer №1

Exploring the impact of altering the type property on an <input> element.

Interestingly, AngularJS behavior is influenced by whether jQuery was added before or after angular.js.

Check out this discussion on the potential to modify the type: change type of input field with jQuery

Additionally, there's a pull request for AngularUI proposing a new directive to support dynamic type changes: https://github.com/angular-ui/angular-ui/pull/371

If the suggested solution doesn't meet your needs (especially if the type remains unchanged post-render), consider using the ng-switch approach to display the appropriate input for the user.

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

Error 405 (Method Not Allowed) detected on a ReactJS component

I am attempting to initiate a call to an endpoint using the POST method. Below is the corresponding code: import React, { Component } from 'react'; import { Input} from 'antd'; import Form from '../../components/uielements/form&a ...

JavaScript - utilizing a confirmation dialog box within a function

My JavaScript code was working well because I received some fantastic solutions here yesterday. I am now wondering if I can enhance this JavaScript with another query. Currently, the query triggers an alert when the number is greater than 199, and it is fu ...

Accessing environmental variables from pug template

Currently, I am utilizing pug to generate static HTML for my own customized static site builder. In my package.json file, the only line of Node.js server code present is: "watch-pages": "pug -O options.json -w pages/ --out _static-website/" However, the ...

Modify the div's max-width attribute based on checkbox selection in HTML

There is a coding challenge involving a checkbox and a div. When the checkbox is checked, the max-width attribute of the div should decrease (causing it to shrink in size), and vice versa. function adjustTableWidth(checkbox) { var table = document. ...

Highlight text when the user is scrolling the page - using JQUERY

Can anyone suggest a way to dynamically underline text as the user scrolls down a webpage? The underline should only be visible while the user is scrolling. I've experimented with animate.css and other plugins, but haven't been able to achieve th ...

Managing browser back button functionality

I've been struggling to find a solution for handling the browser back button event. I would like to prompt the user with a "confirm box" if they click on the browser back button. If they choose 'ok', I need to allow the back button action, ...

Issue: The object identified as #<Object> does not contain the 'toUpperCase' method

I am currently encountering the error Object #<Object> has no method 'toUpperCase' right before the POST request is initiated. Any assistance you can provide would be greatly appreciated! Thank you in advance. function ajaxeo(url, data, me ...

Switching the orientation of an iPhone on Chrome can lead to content being cut off

Currently, I am conducting testing on an iPhone XS running iOS 15.6.1 The website seems to function properly on iOS Safari (although untested on Android), however, a problem arises when using Chrome and rotating the screen from landscape to portrait mode. ...

Is it feasible to adjust the Scrollbar's position?

Is there a way to change the position of a scrollbar on a webpage? Are there any alternative solutions for this issue? <div style="overflow: scroll; overflow-y: hidden"> </div> I am looking to relocate the scrollbar to a different componen ...

Issue with Express/passport - authenticate function not recognized

I am currently learning about passport.js and I came across a blog that explains how to register users: https://scotch.io/tutorials/easy-node-authentication-setup-and-local However, when I run the server and try to load the /register page, I keep encount ...

The value selected is not being shown using uib-typeahead in the user interface

Within my AngularJS application, I am utilizing uib-typeahead to provide auto-suggestions in a text box. As I start typing, I can see the suggestions appearing. However, when I select one of the options, it does not display in the text field (ng-model). I ...

SoundCloud cross-domain requests fail to work in Safari

I'm struggling to receive a json response when calling SoundCloud's API in Safari. var inputSet={url:setUrl}, clientId={client_id:client_id}; $.getJSON( "https://api.soundcloud.com/resolve.json", $.extend(inputSet, clientId), function( data ...

Adjusting the range input alters color progressively

I am trying to create a range input with a dynamic background color that changes as the slider is moved. Currently, the background is blue but I want it to start as red and transition to yellow and then green as the slider moves to the right. Does anyone ...

The absence of a semi-colon in JSLint

I encountered an error message indicating a semicolon is missing, however I am unsure of where to place it. Here is the snippet of code: $('.animation1').delay(350).queue(function(){ $(this).addClass("animate-from-top") }); ...

When the first element of an array is undefined, Angular's ngFor will not render anything

We have an array called stringArray: var stringArray = new Array(); stringArray[1] = 'one'; In Angular, the ngFor directive displays nothing when stringArray[0] is undefined. How can this issue be resolved? ...

Information vanishes upon scrolling and toggling on the page

While working on creating a website, I came across a great template and decided to use it as inspiration. You can check out the template here. However, during the process, I encountered a common UI bug. When a user scrolls down the page, clicks on the "X ...

How can I retrieve the entire innerText when hidden cells have new lines omitted?

Is there a way to preserve new lines in hidden table cells when retrieving the innerText?​ document.addEventListener('DOMContentLoaded', function() { console.log(`This is innerText of visible td: ${document.getElementById("td2").innerText}`) ...

conducting thorough analysis for detecting modifications akin to $watchCollection in Angular2

I have a situation where I am passing an array of objects from my parent component to child components. Even when a new item is added to the array or the property of an existing object changes, it fails to trigger the ngOnChanges for the affected component ...

New Solution for Direct Java Raw Printing in Chrome Browser Post NPAPI Discontinuation

It's common knowledge that NPAPI will soon be eliminated from Chrome. Is there a substitute for the Jzebra/QZ Java plugin that allows for raw printing (sending raw ESC/P commands) to POS printers? Are there any Chrome APIs (based on HTML5 and Javasc ...

Why does the session variable keep changing after two page reloads?

CodeSnippet.php <?php session_start(); msgbox("Confirm your action: Are you sure?", "confirm"); $Result = $_SESSION['id'] ; print "<p id='txt'> </p>"; if($Result == 1 ) echo "Result is true"; elseif ( $Result == 2 ) ...