Styling not being applied to dynamically added Angular Material input

I'm attempting to incorporate Angular Material inputs based on a numerical value provided by the user.

var inputArray = [];
    for(var i=0; i<vm.boxQty; i++)
    {
        inputArray.push("<md-input-container><input type='text'  data-ng-model='vm.trackingNumber'></md-input-container>");
    }
    angular.element('.box-inputs')
    .html(inputArray);

However, once the inputs are added, they do not receive the material design styling.
https://i.sstatic.net/Dw3ZZ.jpg What is the best approach to add these inputs while maintaining the desired styling? Appreciate your help!

Answer №1

Instead of manually adding inputs, I suggest using ng-repeat to dynamically create them.

Here's an example of how you can do it: Let me clarify further what ng-model='input.val' means.

<div ng-repeat="input in vm.inputs">
      <md-input-container><input type='text'  data-ng-model='input.val'></md-input-container>  
</div>

In your controller, initialize vm.inputs as an array with a single object containing a val property:

vm.inputs = [{
    val: null
}];

The val property helps track which input corresponds to which value. To add another input, simply push a new object with a null val in your controller or function like this:

vm.addInput = function () {
        vm.inputs.push({val:null});
 }

You can include additional properties in the input object based on what information you need to manage for each input field.

Check out this demo that demonstrates these concepts: http://jsfiddle.net/fnc2m3uc/1/

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

Get rid of the unnecessary vertical line that is located at the end of the string

I have come across a situation similar to the one demonstrated in this code snippet: http://plnkr.co/edit/eBjenGqgo3nbnmQ7XxF1?p=preview Currently, I am working with AngularJS 1.5.7. The directives used in my input - ( first ) textarea are the same as tho ...

Fetching a destination through the post approach

Typically, we utilize window.location.href="/index.php?querystring"; in JavaScript. Is it possible to transmit the querystring via a POST method without including any form within the document? ...

Cannot locate the state variable

import React, { Component } from 'react'; import { View, Text } from 'react-native'; import axios from 'axios'; class AlbumList extends Component { state = { albums: [] } ; componentWillMount(){ axios.get(&ap ...

Choose the last element that has a particular class

Let's say I am working with an unordered list like this one: <ul> <li class="item"></li> <li class="item"></li> <li class="item"></li> <li class="placeholder"></li> </ul> As par ...

Checkbox designed in a non-traditional style that requires a label in order to complete the

My Bootstrap modal features multiple-choice options for users, presented as checkboxes. However, I want to customize the appearance so that when a user selects an option, it is highlighted rather than shown as a checkbox. To achieve this look, I enlarged ...

Sending JSON data from the primary window to the secondary window in Electron - A step-by-step guide

I am currently working with a set of 3 files. index.html: ... <a href="#" onclick="fetch(function(data) {console.log(data); subWindow(data)})">subWindow</a> ... The fetch() function retrieves a callback in JSON format. subWindows.js: let m ...

When a class that has specific text is clicked on, execute jQuery code

My collection of elements is set up like a "menu" where all items share the same class. It appears as follows: <div id="menu"> <div class="menu_item"> first </div> <div class="menu_item"> second </div ...

Combining all string elements in an array while preserving objects - here's how to do it

As I ponder the complexity of my question, I find myself wondering how to condense an array by merging adjacent strings while keeping objects intact. For example: array = ["I","want","to",[Obj],"come","together"] The desired output would be: ["I want to ...

What is the best way to ensure text in a paragraph remains at the bottom of a div without any additional space below it?

I would like the text and its border to remain fixed at the bottom of the body element. Additionally, I want the text to be positioned right at the edge of the div boundary. @import url(https://fonts.googleapis.com/css?family=Bungee); body { margin: ...

ExpressJS and Angular: Issue with Loading the Index Page

I'm encountering a challenge while working on an application that incorporates expressjs and angular. The setup involves loading angular, bootstrap, and other libraries from the index page. During testing, I added a button <a ui-sref=about>about ...

AngularJS is known for its ability to handle dynamic attributes

My app's main objective is to develop a property editor. The server provides me with a list of properties and their corresponding types: $scope.properties = [ {name: 'property1', type: 'integer', 'value': 123}, { ...

Enhancing Bootstrap 5 with JavaScript

I am relatively new to creating webpages and have recently started working on one using Bootstrap 5. I am looking to dynamically add or remove the "bg-opacity" Bootstrap class from my navbar based on the viewport width. I initially attempted to achieve thi ...

Issue with transforming rotation in Quirks mode

Currently, I'm developing a website in quirks mode which does not allow the use of . In my project, I am facing an issue where CSS3 translate image is not working in IE10 and 11, although it works fine in IE9 due to the limitations mentioned above. Is ...

Retrieve Image Data by Clicking on Canvas at Precise Coordinates

JS Fiddle Link I'm attempting to implement functionality where a canvas with a linear gradient can be clicked, capturing the image data at that specific point. Additionally, I aim to position a yellow picker relative to the click point on the canvas ...

The information provided in a PHP form is not saved in a MySQL database

I am trying to create a form using AngularJS to save person details in phpMyAdmin (mysql). However, when I submit the form, the details are not getting added to the database. Can someone please assist me in resolving this issue? Below is the HTML page cod ...

Why must I handle Access-Control-Allow-Origin issues with XMLHttpRequest() but not with forms?

When attempting to make a request using XMLHttpRequest(), I encounter the Access-Control-Allow-Origin error. However, when I use a form like the one below, no issues arise. Is there something about forms that allows access? What could be the reason for t ...

Building a Complex Form with Checkbox Options to Uncover Essential Fields

I need some help with a multi-step form that includes required fields. My JavaScript knowledge is still developing, so I apologize if my explanation isn't completely clear. Within my form, I have created an optional section using JQuery that reveals ...

Creating an array of images using input fields and showcasing them as a dynamic slider

I'm currently working on a challenge involving creating an array of images using an input field and then displaying the array of images as a slider in JavaScript. If anyone can help me, please provide a solution. Could you kindly share the JavaScript ...

Converting a CSS file into a string by importing it into a JavaScript file

Is it possible to manipulate a style tag loaded from an external CSS file using plain JavaScript? I'm attempting to apply custom styles to a style tag fetched from a CSS file, The issue lies in my struggle to convert the file content into a string ...

Seamless Integration of Hosted Fields by Braintree

I am currently struggling with setting up Braintree hosted fields on my registration form. Unfortunately, there are significant gaps between the fields which doesn't look appealing. Despite referring to the braintree documentation and guides, I find t ...