Using ng-selected directive along with ng-repeat in a select dropdown

Apologies for asking once more. I am having trouble setting a default value for a select element. Here is the code that I have been using:

Plnkr

I am unable to use ng-options in this particular scenario. Your assistance without utilizing ng-options would be greatly appreciated.

Answer №1

In the scenario where the model's value matches the specified option, the usage of ng-selected will function like this:

ng-selected="business_entity == businessConstants"

Assuming the model is defined as follows:

$scope.formData = {business_entity : 'Công ty TNHH'};

Modified Plunker

Answer №2

Make sure to implement the code snippet provided in your controller:

$scope.formData={};
$scope.formData.business_entity = 'Công ty TNHH';

Also, ensure that you remove 'ng-selected' from the option tag in your template.

<select ng-model="formData.business_entity">
  <option ng-repeat="businessConstants in businessConstants" value="{{businessConstants}}">
    {{businessConstants}}
  </option>
</select>

I have made changes to your plnk, please review it.

Answer №3

replace ng-select with an array reference assigned to the ng-model variable

$scope.formData = {
     'business_entity' : $scope.businessConstants[1]
}

Html

<select name="" id="" 
    ng-model="formData.business_entity">
      <option  
      ng-repeat="businessConstants in businessConstants" 
      value="{{businessConstants}}">
                                {{businessConstants}}
                                    </option>
    </select>

Demo

Answer №4

Revise your HTML to

<body ng-controller="ctrl">
    <select name="" id="" 
        ng-options="option for option in businessChoices"
        ng-model="business_entity">
    </select>
</body>

Your options will be dynamically generated and linked to business_entity automatically.

Update the Controller as follows:

function ctrl($scope) { 

  $scope.businessChoices = [
    'Public Limited Company',
    'Private Limited Company',
    'State Organization',
    'Bank',
    'School',
    'Individual',
    'Other'
  ];

  $scope.business_entity = $scope.businessChoices[4];
}

This code establishes the default value by selecting it from the array businessChoices. Ensure you choose the default value from the existing options rather than assigning it directly.

For reference, check out the updated Plunker link

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

Exploring AngularJS for real-time updates from a persistent database

Hello, I'm new to Angular and currently working on an app that requires frequent polling of a URL every second. The retrieved data needs to be stored persistently for access across multiple views and controllers. To handle this, I've placed the ...

Is it possible to send a Word file as an email attachment using PHP's mail() function

When I sent a Word document file via email, it generated an unknown format like asdfADFDF 0000sadfas15454454. The mail function was used to send the email. Below is the code for that. Please review and let me know: function sendfile() { $id = $_REQU ...

Obtain the ID of a YouTube video from an iFrame link using jQuery

Check out this YouTube video: iframe width="560" height="315" src="//www.youtube.com/embed/XbGs_qK2PQA" frameborder="0" allowfullscreen></iframe>` (Hell Yeah! Eminem :P) I only want to extract "XbGs_qK2PQA" from the link provided. Using $(&apo ...

Does Internet Explorer 10 support the FormData object?

I've developed a JavaScript app that enables me to upload images asynchronously, and it works seamlessly in most browsers. However, the infamous Internet Explorer is causing some trouble... To address this issue, I devised a workaround for IE9- versi ...

How come the "colspan" attribute is not functioning properly in my table?

Check out the simple table form I created on jsfiddle. Here is the code snippet: <table> <tr> <td class="field_label">name</td> <td>*</td> <td> <input type="text"> ...

An unexpected 'undefined' occasionally tacked onto 1% of the URLs visitors requested on my website starting from June 12, 2012

Ever since June 12, 2012 at 11:20 TU, I have been noticing strange errors in my varnish/apache logs. At times, after a user has requested a page, I observe a similar request moments later but with the URL string after the last "/" being replaced by "undef ...

AngularJS: The power of dynamic HTTP POST parameter names

Utilizing an API to update profile information allows for the addition of a nickname, email, phone number, or password in the request parameters, which will then be updated in the database. When updating a specific field, such as Nickname: { "nickname": ...

Position the typography component to the right side

Is there a way to align two typography components on the same line, with one aligned to the left and the other to the right? I'm currently using this code but the components are aligned next to each other on the left side. const customStyles = makeSt ...

Improvement in Select2 Change Event: Update the subsequent select2 box options based on the value change in the preceding select2 box

I need assistance with two select boxes, namely Category and Sub-category. My objective is to dynamically alter the available options in the subcategory box based upon the value selected in the category box. Additionally, I would like to load data for the ...

Several challenges with managing date filters and formats in jqgrid

I am facing several challenges with a single column in jqGrid that is meant to handle date information: 1- The date is retrieved from the back-end and displayed as 29/03/2017 00:00:00. When I attempt to format it using formatter: "date", formatoptions: { ...

Ajax is functional, however the server is not responding

After many attempts to resolve the issue with my website, I am turning to this community in hopes of finding a solution. The problem lies in the fact that while the ajax success function appears to be working and shows a status code of 200 in the network ...

Troubleshooting issues with AngularJS $watch not triggering properly

Even though Deep watch has been activated in the factory, it is not triggering. What steps can be taken to resolve this issue and ensure that an event is triggered when the 'name' value changes? Javascript Code: var app = angular.module('a ...

ERROR: No compatible version of jQuery Foundation could be located

Encountering issues while installing Foundation due to conflicts with Jquery. λ bower install foundation bower foundation#x cached https://github.com/zurb/bower-foundation.git#5.5.1 bower foundation#x validate 5.5.1 against https: ...

Updating the value of a Javascript variable from a separate file

Hello there! I have a file named map.php and I need to modify the center value in the code to be based on a different value provided by another JavaScript file called template.js. In the past, I was able to change other HTML values using setAttribute and q ...

The JSX function seems to be malfunctioning, as the function part is not displaying on the webpage as intended

This code snippet is a part of a React component in a project. I have imported a CSS file for styling and have already integrated Material UI. However, the function for the new article is not displaying on the webpage as expected. import "./Widgets. ...

A JavaScript code snippet that stores the total number of bytes read from a CSV file in a variable

I currently have a CSV file located on a web server that is regularly updated with numeric and string data of varying lengths. I am seeking a solution to calculate the total byte values of each row when reading the file, as the byte count is crucial due ...

Exploring the Functions and Applications of Headers in Javascript

After completing a project which involved updating a JSON database from the front end, I am still uncertain about the role of the headers within the AxiosConfig. Can you explain the difference between using axios with and without it? For instance: What s ...

Retrieve the value from a classic ASP page using an if statement and pass it to

Currently, I am attempting to assign a JavaScript value through ASP. foo('RequestMode', '<%=Request.Querystring("Mode")%>') My goal is to achieve something along the lines of: foo('RequestMode', '<%=if (Reques ...

Cloned element does not trigger on click event

When using jQuery 1.10, I encountered an issue where cloning a div containing a button with a click event defined did not retain the click event on the cloned div. I have come across similar questions multiple times, and existing solutions advise using cl ...

Mapping URLs to objects in JavaScript, TypeScript, and Angular4

I have implemented a class called SearchFilter class SearchFilter { constructor(bucket: string, pin: number, qty: number, category: string) { } } When the user performs a search, I populate the filter i ...