"Is there a way to update the value of a variable within $scope when a specific action

Is it possible in Angular to change the value of $scope.complete to false when a specific URL is clicked by the user?

I display a credit card form based on the value of this variable. Initially, the value is set to true or false depending on the data from the server side indicating if there is a credit card on file.

If there is a credit card on file, I want to include a change link. When this link is clicked, the value of $scope.complete should be changed to false and the credit card form will be shown.

This is my current code:

JS

  $scope.complete = false;
  djangoAuth.profile().then(function(data){
    $scope.model = data;
    if ($scope.model.profile.stripe_id.length <= 0)
      $scope.complete = false;
    else
      $scope.complete = true;
  });

html:

<div ng-if="complete == false">
 <!--show form-->
</div>
<div ng-if="complete == true">
 Credit card already on file. <a href="#">Change?</a>
</div>

Answer №1

To update the value of a property in AngularJS, you can bind a function to ng-click and have it modify the property:

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.complete = true;

    $scope.change = function() {
      $scope.complete = false;
    };
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
    <div ng-show="complete == false">
        Add your form here
    </div>
    <div ng-show="complete == true">
         Credit card information is already on file. <a href="#" ng-click="change()">Want to change?</a>
    </div>
</div>

Answer №2

Take a look at this sample demonstrating ng-click in action with archive()

http://jsfiddle.net/dakra/U3pVM

  <a href="" ng-click="archive()">archive</a>

  $scope.archive = function() {
    var oldTodos = $scope.todos;
    $scope.todos = [];
    angular.forEach(oldTodos, function(todo) {
      if (!todo.done) $scope.todos.push(todo);
    });
  };

This example is not mine, but it illustrates how to implement the functionality.

For further details, refer to the documentation:

https://docs.angularjs.org/api/ngTouch/directive/ngClick

Answer №3

JavaScript

$scope.initialized = false;
  djangoAuth.profile().then(function(data){
    $scope.data = data;
    // set default scope
    $scope.initialized = true;
    $scope.changeStatus = function(){
        $scope.initialized = true;
    }
    if ($scope.data.profile.stripe_id.length <= 0)
      $scope.initialized = false;
    else
      $scope.initialized = true;

  });

HTML:

<div ng-if="initialized == false">
 <!--show form-->
</div>
<div ng-if="initialized == true">
 Credit card already on file. <a ng-click="changeStatus()" ng-href="#">Change?</a>
</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

Error: JSON parsing error - Unexpected token U found at the beginning of JSON string while making a POST request

When I make a post request to an API using the submit() function attached to an ng-click directive, sending data in JSON format, it returns an error. The request works fine on postman, so the error seems to be on the client side only. Additionally, the e ...

Choose a specific <div> element by its unique ID and trigger a click event on it as soon as the page loads

There is a div element on my webpage tagged with the ID of #activateeditbutton. I am hoping to have this specific div trigger a click event upon the page's initial loading. In an attempt to achieve this functionality via jQuery, I have written the fo ...

React infinite scroll component fails to trigger the next function

Implementing the infinite scroll feature with react-infinite-scroll-component requires configuring the component as follows: <div id="scrollableDiv" style={{ height: 300, overflow: "auto" }}> <InfiniteScroll dat ...

Tips for resolving a flickering issue that occurs when switching to an input field with our default value / placeholder plugin

In the realm of web development, numerous plugins cater to the implementation of HTML5's placeholder attribute in older browsers. The plugin we have opted for can be found here. Unlike some other alternatives, this particular plugin, with a few modif ...

Updating the default color of selected text within a webpage's content

Is there a way to modify the default blue color that appears when content is selected on a webpage? I am wondering how to change this selection color to a custom color of choice. ...

What is the best way to show instructions immediately upon receipt of a response?

I'm currently working on developing a website similar to ChatGpt using the OpenAI API for GPT. However, there is a delay in generating responses with GPT taking a few seconds. As a result, my code only displays the instruction once the response from G ...

What is the best way to display data retrieved through Ajax, jQuery, and JavaScript on an HTML page

I have successfully used the script below to fetch data from an api endpoint and populate my charts. Now, I want not only to display the data in my charts but also directly output it in my HTML code using something like the document.write() function. How ...

Perform an Ajax POST request to a PHP script

Currently, I am facing a dilemma regarding how to post data from 9 checkboxes to my php script. I came across the following code: $.ajax({ type: "POST", url: "check.php", data: data, success: success, dataType: dataType }); However, I am unsure ...

In Pure JavaScript, an HTML element is added every time the click event is triggered

Hey everyone, I'm facing a small issue and I could use some help fixing it. I need to implement an onclick event that adds HTML code every time it's clicked. I am hesitant to use innerHTML due to its potential risks. Here is the code snippet: bu ...

Confusion in implementing AngularJS code

Here is a UserService that handles user login: this.result = function(credentials) { $http.post('http://localhost:8080/user/login', credentials).then(function (status) { return status; }); }; And here is a Lo ...

Is it possible to dynamically adjust the number of children in an array?

As a beginner in React, I've been exploring ways to manage an array of components from a parent component. My task involves creating a site where I can dynamically add or remove names to a list. However, I'm facing a challenge in figuring out the ...

Tips for viewing images using javascript?

I've been using reactjs to display selected images in real-time. My goal is to have three different image tags for previewing three separate pictures. However, I've run into an issue where selecting "photo2" ends up displaying it in place of "pho ...

Tips for simulating focus on a Material-ui TextField with a button press

My web application features a unique method for indirectly filling in text fields. Since there are multiple clicks involved (specifically in a calendar context) and numerous fields to fill, I want to provide users with a visual indication of which field th ...

Can you explain the concept of the "Regular Expression Denial of Service vulnerability"?

After recently setting up nodejs on a server, I ran a basic npm install command and received multiple messages like the following: $ npm install npm WARN deprecated <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="55383c3b3c3834 ...

The custom directive is designed to trigger its watcher only once and does not continue to monitor changes

I'm trying to trigger an animation with a custom directive called "activate" which I am using as an attribute in my file, partials/test.html <div activate="{{cardTapped}}" > The directive is defined after my app definition in js/app.js myApp. ...

Module cannot be resolved within react-viro

Having some issues running this application and receiving an error message. View the app code on GitHub here: https://github.com/vnovick/pile-blocks-ar I have followed the asset import instructions outlined here: . Despite following the steps correctly, I ...

Is there a way to deliberately trigger an error using Selenium WebDriverIO?

Is there a way to trigger an error using ChromeDriver with Selenium WebDriverIO? I'm not sure if there's a method like browser.fire('error'). ...

Is there a more efficient method to send a large quantity of input element data to an API via a POST request?

My current ASP.NET project is utilizing JQuery and Bootstrap for the front-end development. One section of the project requires users to fill out a form with over 30 input elements, which then needs to be sent to a back-end API. Typically, I would use JQu ...

Is there a way for the React select component to adjust its width automatically based on the label size?

After defining a React select component, it looks like this: <FormControl> <InputLabel id="demo-simple-select-label">Age</InputLabel> <Select labelId="demo-simple-select-label" id=&quo ...

Transforming XML into Json using HTML string information in angular 8

I am currently facing a challenge with converting an XML document to JSON. The issue arises when some of the string fields within the XML contain HTML tags. Here is how the original XML looks: <title> <html> <p>test</p> ...