Connecting AngularJS with select options

I am currently developing a checkout feature that calculates shipping prices based on the selected country. If the user picks United States, it should display US. For any other country selection, it should show Not US

While the shipping function correctly returns US when United States is selected, it still returns US even when another country is chosen.

JS

var app = angular.module("CartApp", ['ngCookies']);
app.controller("CartForm", function ($scope, $cookieStore) {
    var shipping = 0;
    $scope.shipping = function () {
        var e = document.getElementById("countries").value;
        if (e == 'United States') {
            shipping = 1;
            return "US";
        } else if (e != 'United States') {
            shipping = 4;
            return "Not US";
        }
    };
});

HTML

<div ng-app="CartApp">
    <div ng-controller="CartForm">{{shipping()}}
        <select id="countries" name="countries">
            <option selected="selected" value="United States">United States</option>
            <option value="Albania">Albania</option>
            <option value="Algeria">Algeria</option>
        </select>
    </div>
</div>

Answer №1

It appears that you are attempting to combine two different data binding methods. Just starting out with AngularJS, it seems like you are following the same approach without using getElementById()

angular.module('demoApp', []).controller('DemoController', function ($scope) {
    $scope.countries = [ 'United States'
    , 
      'Albania'
    , 
      'Algeria'
    ];
    $scope.shipping = $scope.countries[0];
    $scope.isUS = function () {
        if ($scope.shipping == 'United States') {
            return 'US';
        } else {
            return 'Non US';
        }
    };
});


<body ng-app="demoApp">
    <div ng-controller="DemoController">
        <div>User selected: {{shipping}}</div>
        <div> Is US? {{isUS()}}</div>
        <select ng-model="shipping" ng-options="c for c in countries"></select>
    </div>
</body>

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

What is a reliable method to retrieve the text from the current LI if all LI elements in the list share the

I'm encountering an issue with retrieving the text from LI elements because I have 10 list items and they all have the same class name. When I attempt to fetch the text using the class name or id, I only get the text of the last item. Here is my code ...

The trio of Chrome, Windows Authentication, and XHR working together synergistically

Our team is currently working on an application using AngularJS for the frontend and C# .NET for the backend rest services. As part of our Windows Authentication process, we rely on actionContext.RequestContext.Principal.Identity to retrieve user informati ...

My AngularJS module seems to be malfunctioning

After spending a significant amount of time on this, I am really hoping for some assistance. I am currently working on creating a module that will generate a directive and controller for the header section of my AngularJS site. Despite not encountering any ...

Trigger a page refresh when you revisit it

Currently, I am utilizing Nuxt in SPA mode and my page structure is set up like this: pages ... - users/ - - index - - new - - update/ - - - _id ... I have a page dedicated to displaying a list of users along with a 'subpage' for adding new u ...

Can a nodeJS script be written to automate selecting options and filling out forms in HTML?

Greetings everyone, I am new here so please excuse me if my formatting is not up to par. Currently, I am tackling a project for an internship and after some extensive research, I am stumped on how to automate certain reports in the next phase. In summary ...

Sharing data between React JS components Passing information between different components in React JS

My NavBar.js component contains login information for the logged-in user. It displays "Welcome" along with the user details when the user is logged in. Now, I want to replicate this functionality in the ProductList.js component so that when a user posts a ...

"Troubleshooting: Child Component Not Reflecting Changes in UI Despite Using Angular

My child component is designed to display a table based on a list provided through @Input(). The data is fetched via http, but the UI (child component) does not update unless I hover over the screen. I've seen suggestions about implementing ngOnChange ...

A guide on effectively utilizing the Map datatype in JavaScript

Recently, I've started delving into the world of es6 Map due to its unique features, but I have some reservations regarding pure operations. For example, when removing properties from objects, I usually use the following function: function cloneOmit ...

AngularJS: ng-repeat excluding duplicate keys

Here is a collection of objects I am working with: [{ key:test1 name: name1 }, { key:test1 name: name2 }, { key:test2 name: name3 }] I am currently using ng-repeat to showcase them: <tr ng-repeat=item in list> <td>{{item.key}}</td ...

Having trouble with rendering components in React and Bootstrap?

Attempting to display basic Bootstrap components using React. This corresponds to the index.html file: <!doctype html> <html> <head> <script src="http://code.jquery.com/jquery-2.1.3.min.js"></script> <script src="j ...

Why is my AngularJS Ng-Repeat not showing the content of my array?

In the process of developing a functionality, I retrieve a document from a mongodb collection, store its data in an array, and utilize this array to populate elements on the view. The challenge is that although I can see that I am retrieving the relevant d ...

Retrieve data from ngModel within a custom directive

I'm attempting to retrieve the value of ngModel from my input by utilizing a directive, sadly I'm having difficulty retrieving it. Take a look at my code: angular .module('myapp') .directive('infoVal', myExtractor); ...

Addressing Equity Concerns within JavaScript Programming

I can't figure out why the final line in this code snippet is returning false. Is it not identical to the line above? const Statuses = Object.freeze({ UNKNOWN : 0, OK : 1, ERROR : 2, STOPPED : 3 }); class myStatus extends Object{ co ...

When attempting to use JQuery autocomplete, the loading process continues indefinitely without successfully triggering the intended function

Currently, I am utilizing JQuery autocomplete to invoke a PHP function via AJAX. Below is the code snippet I am working with: $("#client").autocomplete("get_course_list.php", { width: 260, matchContains: true, selectFirst: false }); Upon execution, ...

troubleshoot using Visual Studio Code

Here's the scenario: I need to debug a project using VS Code. The project is usually started by a batch file and then runs some JavaScript code. Now, I want to debug the JavaScript code, but I'm not sure how to do that. If anyone has any instruct ...

Use regular expressions to locate all closing HTML tags and all opening HTML tags individually

My JavaScript function is currently filtering strings by removing all HTML tags. However, I have now realized that I need to perform two separate operations: first, replacing all closing tags with <br>, and then removing all opening tags. return Str ...

Running and halting multiple intervals in Javascript - a guide

Imagine a scenario where I am setting up 3 intervals with times of 500ms, 1s, and 1.5s. When I click on the button for the 500ms interval, I want to stop the other two intervals and only run the 500ms one. The same goes for clicking on the 1s or 1.5s butto ...

Locating items within a complex array and identifying their corresponding positions

I recently found an interesting code snippet from a different question that allowed me to identify an object. However, I am now faced with the challenge of determining the position of that object within the array. Let's take a look at the example belo ...

Is it necessary to perform a specific action if the text within a div matches pre

I've been struggling to make this work properly. Even after removing the AJAX POST function, the issue persists. No alerts or any indication of what's going wrong. Check out the code on JSFiddle: HTML <div class="notification"> ...