AngularJS: Issue with ng-show and ng-click not functioning when button is clicked

I have a specific requirement where I need to display and hide the description of each column in a table when a button is clicked. Here is the visual representation of what I have: the table

In my HTML code, I have defined a button with ng-click as a function that takes a parameter to determine the visibility of the columns.

However, when I click the button, nothing happens. Can someone please help me understand why?

Here's a snippet of my HTML code:

<table>
<tr>
    <td><strong>Data</strong></td>
    <td><strong>tempmin</strong></td>
    <td><strong>tempmax</strong></td>
    <td><strong>umiditatea</strong></td>
    <td><strong>presiunea</strong></td>
    <td><strong>vitezavint</strong></td>
    <td><strong>nori</strong></td>
</tr>
<tr ng-repeat="md in chisinau.list">
    <td class='td'><span ng-show='buton0'>
    {{md.dt * 1000 | date:"MM.dd"}}</span></td>
    <td class='td'><span ng-show='buton1'>
    {{md.temp.min}}</span></td>
    <td class='td'><span ng-show='buton2'>
    {{md.temp.max}}</span></td>
    <td class='td'><span ng-show='buton3'>
    {{md.humidity}}%</span></td>
    <td class='td'><span ng-show='buton4'>
    {{md.pressure}}</span></td>
    <td class='td'><span ng-show='buton5'>
    {{md.speed}}</span></td>
    <td class='td'><span ng-show='buton6'>
    {{md.clouds}}</span></td>
</tr>
<tr>
    <td ng-repeat='buton in butoane track by $index'>
    <button ng-click="fbuton('buton'+$index)">{{buton}}</button>
    </td>
</tr>

Here's a snippet of my controller.js file:

    $http.get('http://api.openweathermap.org/data/2.5/forecast/daily?q=Chisinau&mode=json&units=metric&cnt=10&appid=6fa04faec9c89ba8cf92dd1f76e7d518')
    .success(function(response){
        $scope.chisinau = response;
    });

    $scope.butoane = [
    'buton-data',
    'buton-tempmin',
    'buton-tempmax',
    'buton-umidiatea',
    'buton-presiunea',
    'buton-vitezavint',
    'buton-nori'
    ];

    $scope.buton0 = true;
    $scope.buton1 = true;
    $scope.buton2 = true;
    $scope.buton3 = true;
    $scope.buton4 = true;
    $scope.buton5 = true;
    $scope.buton6 = true;

    $scope.fbuton = function(x){
         $scope.x = $scope.x == true ? false : true;

    }

Answer №1

Providing a brief clarification with the assistance of @a.u.b and xwid

When you supplied a string to your function, you then accessed the scope object x's property within that function.

To allow the scope to interpret your variable as a property, you should utilize brackets [ ]. This is a common issue with objects.

Instead of using $scope.x, try using $scope[x]

Answer №2

When referencing the variable "x" within the $scope, you can use the following code snippet:

$scope[x] = $scope[x] == true ? false : true;

Answer №3

Consider using $scope[x] as shown below:

$scope.fbuton = function(x){
     $scope[x] = $scope[x] == true ? false : true;
}

Alternatively, you can try using $eval (although I'm not certain):

$scope.fbuton = function(x){
     $scope.$eval(x) = $scope.$eval(x) == true ? false : true;
}

Answer №4

Adding to the solutions already given, I can offer an alternative approach to your issue. I've tweaked the implementation slightly, but it will still yield the desired outcome you're seeking. You can view the fiddler here.
I've restructured 'butoane' into an array of objects that will store both the button label and a boolean value to toggle the description.

Controller

$scope.butoane = [{
    label: 'buton - data ',
    show: true
  }, {
    label: 'buton - tempmin ',
    show: true
  }, {
    label: 'buton - tempmax ',
    show: true
  }, {
    label: 'buton - umidiatea ',
    show: true
  }, {
    label: 'buton - presiunea ',
    show: true
  }, {
    label: 'buton - vitezavint ',
    show: true
  }, {
    label: 'buton - nori ',
    show: true
  }];

  $scope.fbuton = function(x) {
    x.show = !x.show;
  }

HTML

<table>
    <tr>
      <td><strong>Data</strong></td>
      <td><strong>tempmin</strong></td>
      <td><strong>tempmax</strong></td>
      <td><strong>umiditatea</strong></td>
      <td><strong>presiunea</strong></td>
      <td><strong>vitezavint</strong></td>
      <td><strong>nori</strong></td>
    </tr>
    <tr ng-repeat="md in chisinau.list">
      <td class='td'><span ng-show='butoane[0].show'>
    {{md.dt * 1000 | date:"MM.dd"}}</span></td>
      <td class='td'><span ng-show='butoane[1].show'>
    {{md.temp.min}}</span></td>
      <td class='td'><span ng-show='butoane[2].show'>
    {{md.temp.max}}</span></td>
      <td class='td'><span ng-show='butoane[3].show'>
    {{md.humidity}}%</span></td>
      <td class='td'><span ng-show='butoane[4].show'>
    {{md.pressure}}</span></td>
      <td class='td'><span ng-show='butoane[5].show'>
    {{md.speed}}</span></td>
      <td class='td'><span ng-show='butoane[6].show'>
    {{md.clouds}}</span></td>
    </tr>
    <tr>
      <td ng-repeat='buton in butoane'>
        <button ng-click="fbuton(buton)">{{buton.label}}</button>
      </td>
    </tr>
  </table>

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

Angular examine phrases barring the inclusion of statuses within parentheses

I require some assistance. Essentially, there is a master list (arrList) and a selected list (selectedArr). I am comparing the 'id' and 'name' from the master list to those in the selected list, and then checking if they match to determ ...

Improved AJAX Dependency

A few days ago, a question was posted with messy code and other issues due to my lack of experience (please forgive the form handling as well). However, I have made some improvements and added context. The main problem now lies in the second AJAX call. Ch ...

Update text displayed on radio button selection using jQuery

Is there a way to change the label text on a selected radio button from "Set default" to just "default" using jQuery? I think I need to use the class radio-default as the selector, but I'm not very familiar with jQuery. <div class="radio-default ...

Is there a way to exclude certain URLs from the service worker scope in a create react app, without the need to eject from the project?

Is there a way to remove certain URLs from a service worker scope in create-react-app without needing to eject? The service worker is automatically generated, and it seems impossible to modify this behavior without undergoing the ejection process. ...

"Utilizing the Ajax ScriptManager for Effective Namespace Management

After incorporating my Ajax webservice into a namespace (MyCompany.Web.MyService), I encountered an issue where the proxy in Javascript is being regenerated as MyCompany.Web.MyService. Is it possible to change the name of the Javascript proxy to just MySe ...

I am experiencing an issue with my Node.js query, as it is not successfully adding a user to my database using

I'm currently working on developing an API using Node.JS, express, and MySQL with the Sequelize ORM. I wanted to do this project properly without relying on the CLI for every task. However, I've encountered a problem where my API fails to create ...

ReactJS displays a collection of items stored within its state

I encountered an issue while trying to display a list of items in my React app with the following error message: Objects are not valid as a React child (found: [object MessageEvent]). If you meant to render a collection of children, use an array instead. ...

How can I display two slides at once in Ionic 2/3 on a wide screen?

I have been searching for a solution that will allow me to display 1 ion-slide if the device screen is small, but if it's larger, then I want to display 2 ion-slides. Unfortunately, I have not been able to find a suitable solution yet. As an example, ...

Find the sum of values in an array of objects if they are identical

[{ingName: "milk", quantity: "1.5", unit: "cups"}, {ingName: "sugar", quantity: "0.25", unit: "cups"}, {ingName: "vanilla extract", quantity: "1.0", unit: "tsp&quo ...

Exploring the capabilities of Socket.IO in Node.js for establishing a connection with an external server

Background: My localhost (referred to as Server A) hosts a node.js server, while an external server running node.js can be found at (known as Server B). Although I lack control or access over Server B, which serves as a dashboard site for an IoT device in ...

Identifying flex-wrap capabilities across different browsers

Currently, I am developing a project that involves implementing a responsive grid using the flex wrap property. However, since my project needs to support IE9 and older versions of Firefox (version 28 and below), I am in need of a way to determine their ...

jinja2.exceptions.TemplateSyntaxError: instead of 'static', a ',' was expected

My current project involves using Flask for Python, and I encountered an error when running the project from PyCharm. The error message points to line 192 in my home.html file: jinja2.exceptions.TemplateSyntaxError: expected token ',', got &ap ...

Enhance the appearance of the jQuery document.ready function

I'm attempting to customize the jQuery document.ready function <html> <head> <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> <script type="text/javascript> $(function() { c ...

What are the steps for launching a Yeoman Angular-Fullstack project?

I'm looking to launch a basic Angular project created with the angular fullstack framework. https://github.com/DaftMonk/generator-angular-fullstack I've attempted: yo angular-fullstack test grunt build After this, I ended up with 2 folders i ...

Check to see if a guest has shown support or followed an outside party

When you already follow a company on Twitter, the "Follow Us" button of that company will automatically turn grey, regardless of the domain. So, how can you determine if User-X is following companies A, B, and/or C based on their Twitter handles? The same ...

Exploring Angular.JS: How to Access Sub-Arrays and Parse Keys

Trying my hand at building something with Angular for the first time, I'm facing an issue with retrieving JSON data. The data is fetched from a SQL database in JSON format and passed to a template using Angular route: .when('/tasks/:TaskID&apos ...

Is it advisable for AngularJS web services to provide a cloned version of a response?

Running a long-polling web service where recent responses are cached. Subscribers are notified when new data is available. Is it best to provide subscribers with a deep copy of the response, or should the data be shared among all subscribers? Or could the ...

Maximizing the potential of views and subviews in AngularJS

I'm a newcomer to AngularJS and facing some challenges with implementing multiple views. Can someone provide guidance on how to achieve this? My goal is to create a file explorer with two columns: the left column displays subfolders and files, while ...

Invoke a fresh constructor within a $get method in Angular's provider

I'm encountering an issue where I am attempting to utilize a function as a constructor inside the `.provider`, but I'm unable to instantiate a new constructor when it's within the `$get`. Here is my provider setup - this.$get = $get; ...

Executing a JQuery function from varying environments

While this question may seem basic, I am having trouble understanding the behavior described. I have written some JavaScript code and I am puzzled why the second call to foo does not work. You can find the code in this JSFiddle link. $.fn.foo = function( ...