1

I know that this question has already some answers/solutions but none of them works for me most probably because this is the first time when I'm trying to implement something using Angularjs.

I have a div (title) that expands some info when it's clicked and I want to change the icon inside of it when that info is visible...

This is my code:

<div class="title" ng-click="view_variables(request)">
    <i class="glyphicon glyphicon-chevron-right"></i>
</div>

And this is what I tried to do, but not working because the div will not show the expanded info anymore:

<div class="title" ng-click="view_variables(request) = !view_variables(request)">
    <i ng-class="{'glyphicon glyphicon-chevron-right':!view_variables(request), 'glyphicon glyphicon-chevron-left': view_variables(request)}"></i>
</div>

Controller code:

$scope.view_variables = function(req){
    if (!req.enabled_variables && !req.disabled_variables) {
        $http.get('/api/files/' + $scope.file_id + '/requests/' + req.id + '/variables')
        .success(function(data){
            variables = data.data;
            req.enabled_variables = [];
            req.disabled_variables = [];
            for (i=0; i<variables.length; i++) {
                if (variables[i].disabled == true) {
                    req.disabled_variables.push(variables[i]);
                } else {
                    req.enabled_variables.push(variables[i])
                }
            }
        });
    }

    req.show_variables = !req.show_variables;
}
1
  • can u just show the controller's code Commented Mar 22, 2016 at 12:23

4 Answers 4

3

The view_variables function doesn't return anything, so it will always be treated as false.

You want something like this:

<div class="title" ng-click="view_variables(request)">
    <i ng-class="{'glyphicon glyphicon-chevron-right':!request.show_variables, 'glyphicon glyphicon-chevron-left': request.show_variables}"></i>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This was easier than expected :)
0

I think the problem is what you have going on in the ng-click attribute. By using "view_variables(request) = !view_variables(request)" are you not calling the view_variables function twice? Also, it seems strange to be assigning a value to a function call.

I would just keep ng-click="view_variables(request)" as you had in the first line of code, then have the view_variables function set a boolean somewhere in scope ($scope.data.view_vars) and have that determine ng-class for your i element.

Good luck!

--EDIT: Now that you've put up your controller, req.show_variables looks like a useful candidate

Comments

0

Calling a function inside ng-class is a bad idea. Why don't you use a flag for it.

eg. inside controller-

$scope.view_variables = function(request){
  //your code

  $scope.isExpanded = !$scope.isExpanded;
};

and in html

<div class="title" ng-click="view_variables(request)">
    <i class="glyphicon" ng-class="{'glyphicon-chevron-right':!isExpanded, 'glyphicon-chevron-left':isExpanded}"></i>
</div>

Comments

0

May be Better this way using ng-show directive:

<div class="title" ng-click="view_variables(request)">
     <i class="glyphicon glyphicon-chevron-right" ng-show="!view_variables(request)"></i>
     <i class="glyphicon glyphicon-chevron-left" ng-show="view_variables(request)"></i>
</div>

You could use ng-if directive like so:

<div class="title" ng-click="view_variables(request)">
     <i class="glyphicon glyphicon-chevron-right" ng-if="!view_variables(request)"></i>
     <i class="glyphicon glyphicon-chevron-left" ng-if="view_variables(request)"></i>
</div>

assuming view_variables(request) returns true or false... maybe could replace it for req.show_variables.

4 Comments

The view_variables function actually does work (toggles request.show_variables and conditionally executes an $http request), so you don't want to call that in an ng-if. The condition for the ng-if will get evaluated for every digest loop, so the results will be non-deterministic.
@Jack A. it's true... then ng-show should be more appropriate?
Anything you bind to something that gets evaluated during every digest cycle (like ng-if or ng-show) needs to have a consistent value. For performance, it's better to bind to a variable rather than a function. Function calls are best left for event handlers like ng-click, since they only get called when the event is triggered, not every digest cycle.
I understand now, that's why I suggest to use req.show_variables, but I don't know his logic... Yes you are right it will make more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.