AngularJS - Issue with model not refreshing after asynchronous call completed

Seeking assistance with an issue in Angular: the select control is not initially choosing the correct value but updates on subsequent changes. I am new to Angular, and my HTML code looks like this:

<select class="form-control" ng-model="job.SalutationId" ng-init="job.SalutationId=0">
    <option value="0">Salutation</option>
    <option ng-repeat="salutation in lookups.salutations" value="{{salutation.Id}}">{{salutation.Name}}</option>
</select>

I'm loading an object via WebApi representing a job and lookup values. The JavaScript function for loading the job data is as follows:

function loadJob() {
    $http({ method: 'GET', url: url + 'jobs/2' }).
        then(function (response) {
            $scope.lookups.salutations = response.data.Salutations;
            $scope.job = response.data.Job;  
        }, function (response) {
            displayJobErrorMessage(response.data || "Request failed");
        });
}

Although the job.SalutationId is set correctly, the select option does not automatically choose the correct value. Any advice on where I might be going wrong would be greatly appreciated.

Thank you,

Carl

Answer №1

It was a simple mistake, typical of a school boy.

AngularJS makes use of the === operator in javascript to confirm both value and type. Since the select control operates on strings, it raised an issue when my scope contained an integer value.

To fix this issue, I converted the lookup ID into a string value within the scope.

This change should not pose any issues on the API end since it will automatically interpret the string as an integer.

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

Issues encountered when packaging React Native iOS application with CocoaPods

Currently, I am in the process of developing an app with the assistance of https://github.com/invertase/react-native-firebase. The recommended method for installation is through CocoaPods, but I have encountered a multitude of issues while trying to archiv ...

Passing Props to a Functional Stateless Component in React

The AuthLinks component should display the notification count passed as the notificationCount prop in the Notifications Component. I need to access the notificationCount value in the AuthLinks component, but it seems like it should be available in AuthLin ...

There is a second image drawn underneath the canvas background image

I have uploaded a background image onto my canvas, which happens to be a detailed map. My intention is to mark specific locations on the map by "drawing" red pins. However, I am experiencing an issue in Google Chrome where the first pin appears beneath th ...

Encountering difficulty in loading webpage following AJAX request

I'm encountering an issue with my webpage that involves an AJAX request taking longer than usual to complete, sometimes over a minute. Surprisingly, this delay isn't causing any major problems. However, I've noticed that while the AJAX requ ...

Performing cross-origin GET request

I need help with a scenario involving a link and some JavaScript code. The setup is as follows: <a id="link" href="https://yahoo.com" target="blank">Link</a> Here is the script I'm using: var security = function() { var link ...

Locate the present position of the slider element

I am currently in the process of creating a website that features pages displayed in a slider format. Each page has its own unique ID, but all share a common class called 'section'. However, I am encountering difficulty in identifying the ID of t ...

How to successfully close a jQuery dialog within a user control

My user control has a list view with a hyperlink (LnkDelete) that triggers a jQuery dialog. Here is the javascript code responsible for this functionality: $('#LnkDelete').live('click', function (e) { var page = $(this).at ...

Issue with Javascript function failing to assign correct value

Here is a JavaScript function I am working with: function editWebsite(IP) { document.getElementById('ctl04_txtIP').value = IP; } The ctl04_txtIP textbox represents the IP address passed through the parameter IP in the editWebsite function. ...

Posting a URL on Facebook

I shared a link on Facebook http://35.154.102.35/index.html#/shareProfile, but instead of redirecting to my link, it redirects to the profile of the person share.profile. How can I fix this issue? HtmL: <div class="share-links"> <a href="h ...

Display different divs based on the number of clicks with the help of ng-show, ng-click, and ng-init

I am looking to display one div by default. Upon clicking a button, I want to show the second div and then upon another click, show the third div. I have experimented with ng-if and ng-show without success. <table class=""> &l ...

Obtaining the result from within the .then() block

Through the utilization of Google's API, I am successful in retrieving and displaying nearby places on the console. router.get('/', function (req, res, next) { // Locating nearby establishments googleMapsClient.placesNearby({ ...

Class selectors can be used for jQuery drag and drop functionality

Seeking help from experienced individuals, as I am new to JS/jQuery. I have come across a script for drag and copy functionality which I have customized to fit my needs. However, the script currently uses "id" selectors and I wish to switch to using "class ...

Ensure that the only element on the page is clickable

How can I check the checkbox without causing an alert to pop up? <div onclick="alert('test')"> Lorem ipsum dolor sit amet consectetur, adipisicing elit. <input type="checkbox"> </div> I attempted using p ...

Capture HTML content as a string in Metor and then transform it into a DOM element at a later time

I need to store a string, such as "<div class='some_class'>some content</div>", in a mongo document. Later on, I want to retrieve this content and convert it into a DOM node. What is the best way to manipulate the content before inse ...

Learn how to effortlessly update models by integrating AngularJS with Django and Django Rest Framework

Here is a JSON representation of a post based on its ID: http://127.0.0.1:8000/update/1?format=json {"title": "about me", "content": "I like program", "created": "2014-11-29T18:07:18.173Z", "rating": 1, "id": 1} I am attempting to update the rating ...

Identifying when a fetch operation has completed in vue.js can be accomplished by utilizing promises

Currently, I am facing a dilemma in my Vue.js application. I am making an API call within the created() hook, but there are certain tasks that I need to trigger only after the API call has been completed. The issue is that this API call usually takes aroun ...

What is the method for initializing fancybox with the precise image index?

My current usage of fancybox involves the following code snippet: $("a[rel=Fancy" + UserId + "].Items").fancybox({ 'autoscale': 'false', 'cyclic': true, 'transitionIn': 'elastic', & ...

Using Node.js to implement pagination with the POKEapi through a loop

Recently, I've been experimenting with the POKEapi to display information on 150 different pokemons. This is my first attempt at creating a node.js app as I'm just starting to learn javascript. The challenge I'm facing now is implementing ...

If there is xx, then yy will follow suit

Hello, I am relatively new to JavaScript and am currently attempting to develop a script using Greasemonkey that will automatically fill out a form on a webpage. Essentially, what I need the script to do is check for a specific error code on the page. If ...

Class for non-invalid feedback in Bootstrap

How can I represent the concept of an invalid input field using a specific class? I am trying to create a form where, under a certain input field, there will be a placeholder indicating that only 3 characters are allowed. If the validation fails, I want to ...