What is the process for including the selected attribute in a dropdown form that is being looped through

I am currently working on a form that dynamically generates multiple widgets based on JSON data. One specific part of the form includes a select dropdown, with some items requiring specific options to be selected by default.

For example:

object 1 {
    tag: "products"
}

In the ng-repeat widget, the select dropdown looks like this:

<select class="btn-success form-control">
    <option value="companies">companies</option>
    <option value="news">news</option>
    <option value="people">people</option>
    <option value="products">products</option>
</select>

If this is object 1, I want the products option to be automatically selected with the selected attribute.


Here's what I have attempted so far, although it has not yielded the desired results:

HTML

ng-repeat="stuff in stuffs"...

<select class="btn-success form-control">
    <option value="companies">companies</option>
    <option ng-if="widget.selectedTag(stuff.tag)" value="news">news</option>
    <option value="people">people</option>
    <option value="products">products</option>
</select>

Controller

this.selectedTag= function(s) {
    console.log(s);

    if (s = 'news'){
        return 'selected';
    }
}

How would you approach solving this issue?

Answer №1

Check out the solution I found here: Initializing select with AngularJS and ng-repeat

<option ng-selected="{{operator.value == filterCondition.operator}}"
        ng-repeat="operator in operators"
        value="{{operator.value}}">

In my specific case, it looks like this:

<option value="products"
        ng-selected="{{stuff.tag == 'products'}}">products</option>

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

Import data from a file into a JavaScript 2D array

Looking for a way to load an array from a text file in JavaScript? Here's what I have: var linePoints = [[0, 3], [4, 8], [8, 5], [12, 13]]; But rather than defining it in the code, I'd like to read it from a text file. The file looks like this: ...

React failing to display changes in child components even after using setState

When I delete an "infraction" on the child component, I try to update the state in the parent component. However, the changes do not reflect visually in the child component even though the state is updated. It seems like it's related to shallow update ...

Utilize Underscore and Mongoose to set documents as the return value of an outer function

Exploring the possibility of using Mongoose and Underscore in conjunction, I'm attempting to achieve something along these lines: var person_ids = [1, 2, 3]; var persons = _(person_ids).map(function(id) { Person.findById(id, function(person) { / ...

A marker popup in React with Leaflet closes immediately upon clicking when using leaflet-pixi-overlay

Currently, I am developing a leaflet map using React and PixiOverlay for better performance while drawing markers. However, I have encountered an issue with handling popups while working on the code below: The Marker's click event triggers correctly ...

Tips for only retrieving specific data from a JSON call

Is there a way to modify this code so that it only retrieves 5 items from the database? I would like to have a "get more" button to fetch another set of 5 items. The current code pulls in all available items: $.ajax({ url: 'getFeed.php', ...

When using ng-repeat, make sure to track by $index for unique

After manipulating an array with 5 values (1, 2, 3, 4, 5), it now contains duplicate data (1, 2, 3, 4, 5, 1, 2, 3, 4, 5). I would like to use ng-repeat with $index to display the data without duplicates. Is this possible? ...

Unable to get the Gtranslate function to function properly within the drop-down menu

Currently, I am using Gtranslate.io languages on my website with flags displayed without a drop-down menu. Everything is running smoothly but now I am looking to enhance the user experience by placing the flags inside a drop-down list. I want English to ...

"Exploring the power of JQuery in adding new elements and accessing the newly appended

I have a code that appends some HTML to a div using JQuery like this: $("#divId").append("<div class='click' data-id='id'></div>"); and I want to access the appended div by clicking on it like so: $(".click").click(functi ...

Creating a new array from an existing array in JavaScript

I am attempting to clean up this particular array of data: [ { "Cpf": null, "Nascimento": null, "Sexo": null, "OnlyPerson": false, "IsFinanc": false, "Senha&qu ...

Creating consistent web page designs: A guide

Having created multiple practice websites, I have successfully published one. However, I am now faced with the challenge of needing to make design changes. If I want to modify the text surrounding a certain element in every page, I find myself having to ...

A guide on incorporating unique font weights into Material UI

Looking to customize the Material theme by incorporating my own font and adjusting the font weights/sizes for the Typography components. I am attempting to set 100/200/300/400/500/600/700 as options for each specific typography variant, but it seems that o ...

How can you efficiently identify and resolve coding errors or typos in your code?

While practicing node.js and express.js, I encountered an issue with finding typos. One such instance was when I mistakenly typed: const decoded = jwt.veryfy(token, config.get('jwtSecret')); instead of jwt.verify. Even though I eventually disc ...

Approach to Methodology - JSON data/result

Just testing the waters with this question for SO. It's my first time posting, and I'm hoping to get some guidance on how to tackle a task that has been assigned to me. I currently have a page with a form containing some basic fields set up. Th ...

NodeJs ERROR: Module not found

When trying to launch an instance running ubuntu with express, I encountered a module not found error that does not occur on my Windows machine. Error Message: node:internal/modules/cjs/loader:1085 throw err; ^ Error: Cannot find module './src/c ...

Tips for inserting characters at the cursor in a contenteditable element?

Is there a way to simulate typing a character in either JQuery or plain JavaScript? I am working with a contenteditable section where I need to intercept user input to replace certain characters (such as straight quotes with curly ones). While I have foun ...

How do I combine Dj3 rotation?

How do I incorporate rotation into this code snippet: <html> <head> <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c8acfb88fbe6fde6fb">[email protected]</a>" data-semver="3 ...

Learn how to display months on a countdown timer and then customize the format with basic JavaScript for a website

Looking to create a countdown page for the upcoming ICC cricket world cup event that displays the remaining days in two different formats: Format #1: 01 months 10 days 10 hours Format 2: 01 hours 20 minutes 10 seconds (If less than 2 days remain) I curr ...

Is there a way for me to delay the return from eval() until a callback is received?

A script in node.js has been created to retrieve JS code from a file and then run it through an eval(). The code that handles passing the JavaScript code to the eval function looks like this: // Read JavaScript code from a file var outputBuffer = '&ap ...

The organizational chart function was functioning properly on the local environment, however, it failed to work after deployment, resulting in the error message "jQuery(

Currently, I am in the process of creating an organizational chart using the Angular library called orgchart. The code I have developed works fine on my local machine, but when we deploy it onto our nginx server, we encounter an error related to jQuery. T ...

Is there a way to update the value of an <input> element dynamically?

There is an input in a datalist that I am working with. <input type="text" value="1" id="txtcount"> I am trying to obtain the new value of the input when the text changes. I attempted to use the following code, but it did not work as expected. &l ...