Using AngularJS to send a model to ui-select

Here is the scenario at hand:

  1. A form includes a directive known as
    <timeZone ng-model="formModel.timeZone"
  2. This directive communicates with a web service to fetch timezone information
  3. The directive then presents the timezones in a ui-select dropdown

The form itself does not concern itself with the list of timezones; all that logic is encapsulated within the directive. Its main objective is to handle the selection and storage of the chosen timezone.

Code - Form:

<time-zone name="preferredTz" ng-model="profileDetails.preferredTz" ng-required="true"></time-zone>

Code - Directive (JavaScript):

.directive('timeZone', function (staticCommonResourcesAPI) {
        return {
            restrict: 'E',
            require: 'ngModel',
            templateUrl: 'shared/partials/timezoneField.tpl.html',
            link: function(scope, element, attr, ngModel){
                scope.timezones = [];
                staticCommonResourcesAPI.getTimezones().then(function (response) {
                    scope.timezones = response.payload;
                });
            }
        };
    });

Code - Directive (HTML Template)

<ui-select
        id="timezoneCode"
        name="timezoneCode"
        required="true"
        theme="bootstrap"
        ng-disabled="disabled || signupStatusTag">
    <ui-select-match>{{$select.selected.code}}</ui-select-match>
    <ui-select-choices repeat="timezone.code as timezone in timezones | filter: $select.search" class="ui-select-override-top-style">
        <span class='no-underline'>{{timezone.name}}</span>
    </ui-select-choices>
</ui-select>

To properly integrate profileDetails.preferredTz with uiSelectModel.selected, it seems that attention should be given to how ui-select functions.

Attempts have been made using scope : true, but thus far it has resulted in either the ui-select utilizing an entirely different model or the profileDetails.preferredTz being set to undefined upon initialization.

Answer №1

Consider utilizing nForm for your project

<ng-form timezone ng-model="myModel">

 ...uiSelect

</ng-form>

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

Utilizing a form on numerous occasions prior to its submission

As a newcomer to JavaScript, I am exploring the best approach for a specific task. The task involves a form with checkboxes representing different music styles and a selector for names of people. The goal is to allow users to select music styles for mult ...

I am having difficulty in crafting a sign-up form accurately

In my HTML file, I have a box that includes a sign-up form: <!-- sign up form --> <div id="cd-signup"> <form class="cd-form" action = "signup.php" > <?php echo "$error" ?> <p clas ...

Modify the color of every element by applying a CSS class

I attempted to change the color of all elements in a certain class, but encountered an error: Unable to convert undefined or null to object This is the code I used: <div class="kolorek" onclick="changeColor('34495e');" style="background-c ...

Modifying the 'child' node within a JSON object

Exploring the implementation of d3s Collapsible tree using a custom node hierarchy. My dataset structure deviates from the norm, for example: http://jsfiddle.net/Nszmg/2/ var flare = { "Name": "Example", "members": [ { "BName":"Ja", ...

Issue with displaying PDF files on Google Chrome due to a software glitch

Recently, I encountered a puzzling issue on Google Chrome. I am not sure if the error lies with Google or within my code. When I set the div as display: none; and then show it again, the PDF view only shows a grey background. However, if I zoom in or out, ...

The React Material Component stubbornly resists being horizontally aligned in the Code Sandbox

Currently, I am working on getting my Material design to function properly within the CodeSandbox environment. One issue I am encountering is attempting to center it horizontally. As of now, it appears like this: To make it easier to identify its locati ...

Incrementing and decrementing a textbox value by one using onClick

I'm looking for help with a JavaScript solution without using jQuery or any plugins, specifically for Cordova/PhoneGap. I am new to JavaScript and learning as I go, so please bear with me. My goal is to create a functionality where there is a textbox ...

Ensure that the initial section of the page spans the full height of the browser, while all subsequent sections have a

I have a website composed of various blocks with different heights, all extending to full width. My goal is to make the first block the full height and width of the browser window, while keeping the other blocks at a set height as seen on this site: After ...

Is there a way for me to retrieve the pageProbs that I have shared with a Component in _app.js?

Upon the loading of my website, I am fetching a large amount of data using getInitialProps. MyApp.getInitialProps = async (appContext) => { // Calls page's `getInitialProps` and fills `appProps.pageProps` const appProps = await App.getInitialProps( ...

Create a custom loading spinner for a jQuery AJAX request

How can I add a loading indicator to my Bootstrap modal that is launched from a link? Currently, there is a 3-second delay while the AJAX query fetches data from the database. Does Twitter Bootstrap have built-in functionality for this? UPDATE: Modified J ...

Stuck with the Same Theme in the AppBar of material-UI?

Currently, I am attempting to modify the theme of a material-UI AppBar by utilizing states. Strangely enough, although the icons are changing, the theme itself is not. If you'd like to take a look at the code, you can find it here: https://codesandbo ...

Getting access to a variable on the client side using express-expose

I'm looking to assign a JavaScript variable on my index.html page once it's returned by express.js. I've attempted to utilize the express-expose middleware, but I'm struggling to figure out how to set the variable in the static HTML pag ...

Sending two objects back in res.send() in an API: A step-by-step guide

I've got an API that looks like this router.get('/exist', async (req, res) => { try { const { user: { _id: userId } } = req; const user = await User.findById(userId); const profile = await Profile.findById(user.profile, &apo ...

Using VueJS for reactive binding效果

I am attempting to assign a class using the following syntax: :class="{active: favs.medium_title.fontWeight === 'bold'}" However, the fontWeight attribute is not yet defined when the component loads. This is an excerpt from my object: favs: { ...

Efficient method of delivering cohesive information from database to user without the need for continuous querying

Within the database, there is stored data about each user that typically remains constant. However, occasionally a user may update their information, such as changing their name. This data includes the user's name, username, and company details. The ...

Sharing an image from an Ionic app to an Express.js server using Multer

I've attempted various methods to achieve this, but with no success. Here is my code for uploading an image using the Ionic framework (simply copy and paste it to run on Android). angular.module('starter.controllers', ['ngCordova' ...

``Is there a more SEO-friendly option instead of using an iframe?

I am looking for a solution to easily share my content with other websites without the issues I currently face. Presently, I use an iframe which poses two problems: <iframe width=“540”; height=“700” frameborder=“0” src=“http://www.energi ...

Error in code - message gets sent immediately instead of waiting for timeout to occur

There seems to be an issue with the code where the message is sent instantly instead of waiting for the specified timeout duration. Based on the code, it should wait for the time mentioned in the message. I'm puzzled as to why it's not functioni ...

Implementing image max-width and maintaining aspect ratios for responsiveness

Within the code snippet below and utilizing Bootstrap, a grid layout is defined with several .block components, each comprising an image and a title. The images are designed to be larger than the column size so that they can expand to full width for respon ...

Tips for breaking up array elements across multiple "tr" tags

In this particular example, I have a Fiddle set up with multiple tr elements displayed within an ng-repeat. My goal is to have three td elements per row. Would it be necessary to use an inner angularjs ng-repeat and split the iconDets array for this purpos ...