Angular toggle variable in ng-click doesn't work


amnesia

I have a table that lists customers, and for customers that also have a list of customers, the user can click on the table row to display that list. HTML looks like this:

        <tbody ng-repeat ="item in customers | clientSettingsFilter:customerId">
            <tr ng-class="{'row-hover': item.clientSettings.length>0}" ng-click="item.showClients=!item.showClients">
                <td><span ng-class="{true:'glyphicon glyphicon-chevron-down', false:'glyphicon glyphicon-chevron-right'}[item.showClients]"  ng-hide="item.clientSettings.length==0"></span></td>
                <td>{{item.id}}</td>
                <td>{{item.name}}</td>
                <td><button type="button" class="btn btn-default btn-xs" ng-click="go('/client-config/defaults/'+item.id)">Defaults</button></td>
            </tr>
            <tr ng-show="item.showClients">
                 ..... // client data

My weird behavior is this:

If I don't define the 'showClients' property in the customers dataset, everything works as expected, but the chevron icon is not displayed at first. After one click, it shows up and the toggle works as expected. I thought it might be because ng-class is looking for true or false, and undefined doesn't satisfy both.

If I predefine the showClients property to true or false, the chevrons are displayed correctly on page load and the client list is displayed correctly, but the toggle doesn't work anymore, like ng-click doesn't do anything or fails for some reason Change the value. I'm not sure how to debug inline directives like this.

edit

As requested, here is the relevant event code from the controller:

filter('clientSettingsFilter', function () {
    return function (customers, customerId) {
        var filtered = [];

        if (!customerId)
            return filtered;

        customerId = customerId.toLowerCase();

        angular.forEach(customers, function (item) {
            if (item.id.toLowerCase().indexOf(customerId) !== -1) {

                // format some text properties, omitted for brevity

                // if this line is uncommented, the ng-click does not work
                //item.showClients = false;
                filtered.push(item);
            }
        });

        return filtered;
    };
});
paw CAT

The condition you are using ng-classwill only add something if the value is trueor and nothing if the value falseis undefined.

Instead use the more verbose ternary operator:

ng-class="item.showClients ? 'glyphicon-chevron-down' : 'glyphicon-chevron-right'"

It's better to move the class glyphiconto a normal classproperty:

class="glyphicon"

Demo : http://plnkr.co/edit/bxgp4HyFkOygc0foxAKN?id=2 _ p = preview

item.showClients = false;The behavior you are seeing when uncommented in the filter is due to how the digest loop works.

If it item.showClientsis false, and then clicked, trthe following happens (somewhat simplified):

  1. The expression in ng-clickwill execute, set item.showClientstotrue
  2. The digest cycle will begin
  3. The filter will run and set again item.showClientstofalse

Filters are used for filtering, not for modification.

Also note that using a filter with a ng-repeatdigest filter triggers every digest loop, and since each digest loop consists of multiple digest loops (at least two digest loops), it's important to keep the filter simple, otherwise They will have a bad effect on performance.

Related


Angular toggle variable in ng-click doesn't work

amnesia I have a table that lists customers, and for customers that also have a list of customers, the user can click on the table row to display that list. HTML looks like this: <tbody ng-repeat ="item in customers | clientSettingsFilter:customerId">

Toggle display button with click() doesn't work in Angular

Suhan Sud I have files named server.component.ts and server.component.html. My server.component.ts file looks like this. import { Component } from '@angular/core'; @Component({ selector: 'app-server', templateUrl: './server.component.html', styles : [

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

ng-click doesn't work in angular-ui-grid

username I'm trying to fire a click event when a cell in ui-grid is clicked but ng-click is not working. Also ng-class doesn't work, I can't get width: "*" works fine. Does anyone know any accurate documentation for angular-ui-grid ? I am using angular v 1.5.1

Angular-js: ng-click doesn't work

Shiva Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file: <html ng-app="MyApp"> <head> <title>Angular Demo App</title>

ng-click doesn't work in angular js

Himanshu N Tatariya I am using Angular js from one of my projects and calling the "ng-click" function as follows <a ng-click="logout'{{x.ParentEntityId['#text']}}')" target="_blank">{{x.Title["#text"]}}</a> In "x.ParentEntityId['#text']" the value is "7560183

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

Angular-js: ng-click doesn't work

Shiva Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file: <html ng-app="MyApp"> <head> <title>Angular Demo App</title>

Angular JS ng-click doesn't work in child element

Tantrop I'm trying to toggle a variable when an element in the DOM is clicked, but I'm getting some weird behavior. Full example is here Essentially, if I click ng on the .options div and then leave the controller on the .options-tab div, the event fires (but

Calling function with angular ng-click doesn't work

Program 345 I am trying to switch the view from one page to another when a button is pressed. I'm using ng-click but it doesn't switch views when the button is pressed .js file $scope.onCreateAppointment = function () { $scope.templates.myTempla

ng-click doesn't work in angular js

Himanshu N Tatariya I am using Angular js from one of my projects and calling the "ng-click" function as follows <a ng-click="logout'{{x.ParentEntityId['#text']}}')" target="_blank">{{x.Title["#text"]}}</a> In "x.ParentEntityId['#text']" the value is "7560183

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

Angular ng-click doesn't work with $compile

freddy83 I have code similar to the following code to trigger events in an clickAngular app . Why doesn't the event fire? var app = angular.module("myApp", []) app.directive('myTop',function($compile) { return { restrict: 'E', template: '<div></div>',

ng-click doesn't work in angular-ui-grid

username I'm trying to fire a click event when a cell in ui-grid is clicked but ng-click is not working. Also ng-class doesn't work, I can't get width: "*" works fine. Does anyone know any accurate documentation for angular-ui-grid ? I am using angular v 1.5.1

ng-click doesn't work in angular-ui-grid

username I'm trying to fire a click event when a cell in ui-grid is clicked but ng-click is not working. Also ng-class doesn't work, I can't get width: "*" works fine. Does anyone know any accurate documentation for angular-ui-grid ? I am using angular v 1.5.1

Angular-js: ng-click doesn't work

Shiva Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file: <html ng-app="MyApp"> <head> <title>Angular Demo App</title>

Angular-js: ng-click doesn't work

Shiva Since I'm very new to angular js, I'm trying to develop small applications, but I'm stuck because ng-click is not working. please help. Thanks in advance. Here is my code: HTML file: <html ng-app="MyApp"> <head> <title>Angular Demo App</title>

Calling function with angular ng-click doesn't work

Program 345 I am trying to switch the view from one page to another when a button is pressed. I'm using ng-click but it doesn't switch views when the button is pressed .js file $scope.onCreateAppointment = function () { $scope.templates.myTempla

Angular JS ng-click doesn't work in child element

Tantrop I'm trying to toggle a variable when an element in the DOM is clicked, but I'm getting some weird behavior. Full example is here Essentially, if I click ng on the .options div and then leave the controller on the .options-tab div, the event fires (but

ng-click="tab=$index" doesn't work in angular

cubic I have this in my app: <li ng-repeat="name in tabs track by $index" ng-class="{selected: tab==$index}" ng-click="tab = $index">{{name}}</li> and it doesn't work, when i click on the item the selected class is enabled (every li clicked has the class, cli

ng-click doesn't work in angular js

Himanshu N Tatariya I am using Angular js from one of my projects and calling the "ng-click" function as follows <a ng-click="logout'{{x.ParentEntityId['#text']}}')" target="_blank">{{x.Title["#text"]}}</a> In "x.ParentEntityId['#text']" the value is "7560183

Toggle fontawesome icon on click, doesn't work

Paula I need a fontawesome icon to change to another fontawesome icon when clicked. I have an "empty" heart icon that needs to be changed to a "full heart" when clicked. I've searched for different JS scripts and none seem to work, so I'm clearly doing somethi

Toggle fontawesome icon on click, doesn't work

Paula I need a fontawesome icon to change to another fontawesome icon when clicked. I have an "empty" heart icon that needs to be changed to a "full heart" when clicked. I've searched for different JS scripts and none seem to work, so I'm clearly doing somethi

Toggle fontawesome icon on click, doesn't work

Paula I need a fontawesome icon to change to another fontawesome icon when clicked. I have an "empty" heart icon that needs to be changed to a "full heart" when clicked. I've searched for different JS scripts and none seem to work, so I'm clearly doing somethi

toggle doesn't work after click

Chris Is there anyway I can keep the div with a black background once enabled? Basically, I have: <div class="navigation-box"> <div class="sidehead"><i class="lock"></i><span class="title">User Account</span> <span class="toggle">+</span></div> <ul