Creating a visual illustration with the simple click of a button utilizing AngularJS

I am trying to create a button that, when clicked, will draw a circle on the screen.

Eventually, I plan to expand this functionality to draw multiple circles in specific locations, but for now, I just need it to draw one circle.

The issue seems to be with the JS code where $scope.draw=function(ncircs) is written.

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


app.controller('MainCtrl', function($scope) {

    $scope.graph = {'width': 1000, 'height': 1000};

    $scope.circles = [

        JSON.parse("{\"x\": 85, \"y\": 20, \"r\":15}"),

        {"x": 20, "y": 60, "r":20},

        {"x": 18, "y": 10, "r":40}
    ];

        $scope.draw=function(val)
        {
            $scope.circles.push(JSON.parse('{\"x\":'+val+', "y": 220, "r":30}'));
        };

    $scope.rectangles = [

        /*    {'x':220,  'y':220,  'width' : 300, 'height' : 100},
        {'x':520,  'y':220,  'width' : 300, 'height' : 100}
        */
    ];

}
);

angular.bootstrap(document.getElementById('body'), ["app"]);

HTML code:

<div id="body">
<div ng-controller="MainCtrl">
    <label>Num questões:</label>
    <input Id="NumQuest" class="span3" style="margin: 0pt auto;" type="text" placeholder="Num questões..." data-provide="typeahead" data-items="1"
           />

    <p><button ng-click="draw(NumQuest)">Draw</button></p>


    <svg ng-attr-height="{{graph.height}}" ng-attr-width="{{graph.width}}">

    <circle ng-repeat="circle in circles"

            ng-attr-cx="{{circle.x}}"

            ng-attr-cy="{{circle.y}}"

            ng-attr-r="{{circle.r}}">
    </circle>

     <rect ng-repeat="rect in rectangles"

           ng-attr-x="{{rect.x}}"

           ng-attr-y="{{rect.y}}"

           ng-attr-width="{{rect.width}}"

           ng-attr-height="{{rect.height}}">

     </rect>

     </svg>
 </div>
    </div>

I'm having trouble getting the draw button to work properly. When using the Id of the input button as a parameter, nothing happens. I've tried different methods like $NumQuest, NumQuest.value, etc., but none seem to work...

Any help would be greatly appreciated...

Bruno

Answer №1

If you're facing the same issue I did, here's how to solve it:

The key is to access the JavaScript value from the element ID like so:

$scope.draw=function(val)
        {
            val = document.getElementById("NumQuest").value;
            $scope.circles.push(JSON.parse('{\"x\":'+val+', "y": 220, "r":30}'));
        };

By doing this, everything should function correctly as long as you simply use:

Draw

within the HTML code! It's a straightforward and efficient solution!

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

How can I pass a value into a form within a modal in Bootstrap?

I am currently in the process of developing a university search website. When users enter a keyword, it will return results and display them in cards. Each card will have an 'Add review' button. Upon clicking this button, a modal will pop up cont ...

Creating a Kendo UI grid within a Kendo UI window and utilizing Knockout JS bindings

I'm encountering an unusual issue while working with Kendo UI and Knockout JS libraries. When attempting to display a kendo grid within a kendo window, the rows inside the grid are being duplicated. Below is a snippet of the code I'm using: JS: ...

What could be causing the slowdown in my NodeJS IRC-bot?

Decided to challenge myself today by creating an IRC-bot using JavaScript with NodeJS. So far, everything is functioning correctly, but it seems like there may be a bottleneck somewhere. For instance, when I input a command multiple times, the initial res ...

I'm having trouble getting the activeStyle to work properly with a <NavLink>

I'm facing an issue with setting activeStyle for the NavLink component when the URL is on the map route. Currently, when I click on a NavLink, I can navigate to the correct route, but only <NavLink to='/' is being highlighted as active. ...

Is the authentication process different when developing an application using Angular?

Typically in a traditional web application, users log into the system by saving an encrypted cookie that is sent with each request. The backend decrypts this cookie and utilizes identifiers like user_id or guid to retrieve user information. When authentic ...

Error: Undefined Function [Thinkster.io's Angular Tutorial Chapter 4]

Currently working on the Angular Tutorial provided by Thinkster.io and enjoying every bit of it. However, I've hit a roadblock in Chapter 4 that seems impossible to overcome. Whenever I attempt to execute a Post or Delete action, I encounter the follo ...

Attempting to update the appearance of each item within an array by utilizing the ForEach() method

I am working on a function that dynamically creates new divs based on a specific condition. Once these divs are created, I store them in an array using the .push() method like so: function SnakeBody(){ BodySnake = document.createElement("div"); tabulei ...

Tooltips for Images in Vega Charts

As a developer skilled in d3, I am navigating the world of VEGA charts and seeking guidance on how to incorporate an image in a tooltip. Do you have any suggestions for me? For example, considering this particular scenario: If we assume there is an addit ...

Attempting to transmit JavaScript information to my NodeJS server

Having some trouble sending geolocation data to NodeJS through a POST request. When I check the console log in my NodeJS code, it's just showing an empty object. I've already tested it with postman and had no issues receiving the data. The probl ...

assigning a CSS class to an input element based on a certain condition

My dilemma involves an input field for which I need to conditionally apply a CSS class. For instance, if firstName == undefined, I want to apply the CSS class ng-dirty. To achieve this, I attempted: <input required [(ngModel)]="customer.firstName" nam ...

Attempting to nest an AJAX request within the success callback of another AJAX request

I am attempting to execute two jQuery ajax calls, with the second call being made from the success callback of the first. I have experimented with different variations of the code, such as adjusting the brackets. Below is my attempted code snippet: $.aja ...

Transform from a class-based component to a function-based component

Currently experimenting with AutoComplete and AutoFill features in React. My goal is to transition the code into using React hooks, as I have primarily used hooks throughout my project. I've made some progress in converting it to a hook-based struct ...

The art of replacing material-ui styles with styled components

As a newcomer to UI material design, I am eager to create my own customized Button Component using styled-components. I am facing a challenge in overriding the CSS based on different button variations such as "primary" or "secondary". You can find my cod ...

What is the best way to verify that only numbers are entered using the `ng-pattern` attribute

My attempt to use the ng-pattern attribute to only allow numbers in my input field is not working as expected. Despite specifying this pattern, I am still able to enter characters after the number. How can I prevent this from happening? Below is the code ...

Angular tabs: fetching tab content with $http upon clicking

My project involves managing big forms with a lot of data, and I've been thinking about organizing the information into tabs with separate sections for each tab. I'm looking for a solution where the content of the tabs is only loaded when clicke ...

Framework7 disables the jQuery.appear plugin

I am utilizing Framework7 and aiming to incorporate jQuery.appear for executing a script once an element becomes visible to the user. The implementation is successful when using this code in a basic HTML setup: <!DOCTYPE html> <html> < ...

Accessing data in vuex can result in Firebase SnapShot.val() returning null

I am developing an application that allows access for students, staff, and non-teaching staff. Here is how my form data is structured: formData: { name: "", email: "", password: "", select: null }, options: ["Student", "St ...

Automatically populate input field with value from a different input field

I'm facing an issue: <table> <?php $data['criteria'] = array('GPA', 'SEMESTER', 'INCOME', 'OTHER SCHOLARSHIP'); $criteria = array(); foreach ($data['criteria& ...

The concept of undefined functions and the use of dependency injection may not always align

Recently starting with AngularJs, I am honing my skills by developing a single page Todo Application. However, I have encountered an issue while trying to load a localStorage factory that I intend to use for this project. Currently, I am stuck on the error ...

Error: The variable "Tankvalue" has not been declared

Is there a way to fix the error message I am receiving when trying to display response data in charts? The Tankvalue variable seems to be out of scope, resulting in an "undefined" error. The error message states that Tankvalue is not defined. I need to ...