Angular save function not updating the boolean value

One of the challenges I'm facing is with the scope in my controller. Here's what it looks like:

$scope.carprice.active = true

In my view, there are two radio buttons that have the following code:

ng-click="carprice.active=true"

ng-click="carprice.active=false"

I need to figure out how to modify the value of the scope properly. Currently, even after selecting the false option, it still saves as true in the database.

I've also attempted using the click as a model, but that didn't solve the issue either.

Any suggestions would be greatly appreciated. Thanks, Sam

Answer №1

To achieve this functionality, it is recommended to utilize the ngValue directive instead of ngClick. You can bind the radio buttons to the same model, carprice.active:

<script src="https://code.angularjs.org/1.4.8/angular.js"></script>
<script>angular.module('demo', [])</script>

<div ng-app="demo" ng-init="carprice.active = true;">
    
    <input type="radio" ng-model="carprice.active" ng-value="true"> Active
    <input type="radio" ng-model="carprice.active" ng-value="false"> Not active    
    
    <pre>{{ carprice.active }}</pre>
</div>

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

Attempting to verify the presence of an element within an array and then making updates to the array

My current goal is to create a function called toggleStar, which will allow users to select an item from a list of JSON objects and add it to another list, known as the Favorite list. I attempted to use indexOf in my code, but it doesn't seem to be f ...

Determine the scope of an element in Javascript in relation to its parent container

I have a function that returns an array with two elements, however it won't work in IE. The function returns the HTML code of what the user selects inside a div (with id=text). It also returns the range of the selection. If the user selects a simpl ...

JavaScript application that features an audio player complete with a dynamic progress bar and customizable tags

Looking to create a custom audio player using HTML, CSS, and JS. The player should have basic functionality like a play button and progress bar. However, I want to allow users to add tags on the progress bar of the audio file, similar to how SoundCloud&apo ...

Having trouble reading the file using jQuery in Internet Explorer 8 and earlier versions due to its non-XML format (albeit resembling XML)

Currently, I am utilizing AJAX to load a KML file (which essentially functions as an XML file). The parsing works seamlessly in IE9, FF, and other browsers, but encounters issues in IE8. Although the data is retrieved, I face difficulties parsing it in jQu ...

How can I create an accordion panel that can be toggled using

I've been working on creating an accordion panel using CSS with a touch of JavaScript. Everything seems to be functioning correctly except for the toggling of the panels. Essentially, when one panel is clicked, I want all other open panels to close. ...

Utilizing Pug for Passing Variables to JavaScript

I am working with a Pug view that displays a set of links to users. The user is already authenticated and their username and department are stored in session variables. I am able to pass these variables to the view using this code: res.render('landin ...

Understanding the visibility scope in JavaScript's object-oriented programming

I have the following code snippet: function A() { this.AFunction = function() { var b = new B(); b.BFunction(); } } function B() { this.BFunction = function() { // some code $.ajax({ url: url suc ...

"Efficiently handle JSON and binary data passing with the enhanced functionality of Express Body Parser

Within my express app router, I have routes set up to handle both POST requests with JSON data and binary data. The issue arises when I use body parser to parse the JSON data, as it incorrectly interprets the binary data as JSON and causes errors during th ...

Are Angular global variables used in the same way as $scope in terms of visibility and usage?

So, I recently discovered that Angular allows for the use of global variables: Check out Angular Values var app = angular.module('mySuperDuperApp', ['SomeExternalModule']); // Defining some global variables here app.value('globa ...

Tips for populating an input field with a variable value

Is there a way to automatically fill an input field with a specific variable value? For instance, if the number is set to 10 and a user clicks a button, can the input field be prepopulated with the number 10? var number = 10; <input type="number"> ...

What is the technique for retrieving the data-id value and transferring it to a different page with jQuery's post method?

Here is the code I'm working on: <a href="#view_contact" class="btn btn-info btn-xs view" data-id="<=$row['ADMINISTRATOR_ID'];?>" data-toggle="modal">View</a> I'm trying to fetch the value of data-id and pass it to a ...

What is the method for adding an item to an array in JavaScript?

actions = [{ 'name' : 'Share', 'icon' : 'fa fa-share-alt library_icon mylibrary-icon-right' },{ 'name' : 'Edit', 'icon' : 'fa fa-pencil-square-o library_icon mylibrary ...

Tips for Accessing the TextInput Content in React Native

My code is currently experiencing a bug where I am getting an undefined object type when trying to console log user input. Below is the code snippet I am working with. Can you help me identify and correct the issue? export default function App() { con ...

Unlocking the potential of GraphQL: Harnessing the power of sibling resolvers to access output from another

Could use a little assistance. Let's say I'm trying to retrieve the following data: { parent { obj1 { value1 } obj2 { value2 } } } Now, I need the result of value2 in the value1 resolver for calculation ...

Troubleshoot: Bootstrap Tooltips visible in DOM but not functioning

I am currently attempting to utilize bootstrap's tooltip feature on a dynamically created set of divs. When hovering, I can see in Chrome's inspector that the div itself is being modified correctly (title value is passed to data-original-title) a ...

How can aoColumnDefs be configured in jQuery DataTable without the need to instantiate a new DataTable object?

When it comes to setting column names, I am familiar with using the following method: var table = $('#'+String(response.chartID[i])).DataTable({ stateSave: true, aoColumnDefs: aryJS ...

Exploring the Safari browser with Polymer 2.0

Looking for some help with a question I have... I've been experimenting with the new Polymer 2.0 preview on Safari, but it doesn't seem to be working correctly. I'm using a simple code (just my-element) in the index.htm file, loading the pol ...

Ways to extract the value from a jQuery object

How can I retrieve the selected time value using a jQuery plugin and save it on a specific input element within the page? Refer to the plugin and code provided below: http://jsfiddle.net/weareoutman/YkvK9/ var input = $('#input-a'); input.cloc ...

ECharts - Version 3.1.6 Event Plugin

I am looking for advice regarding the management of two charts with reference to echars from Baidu (version 3.1.6). Is there a way to control both charts by engaging in a click event on one of them? In simpler terms, how can I capture and respond to the c ...

Troubleshooting problem with strokeRect on canvas following a CSS transform rotate(90deg)

English is not my strong suit, so please bear with me. Let me outline the issue I'm currently grappling with. I'm in the process of developing a function that allows users to draw a rectangle on a canvas using their mouse (specifically, using t ...