4

I am trying to use ng-class and bind a class to an expression so, that I can unit test the expression binding. But, it seems that I am missing something.

The button:

<li><a class=""  ng-click="onAddInterface()"><i class="icon-plus-sign"></i> add interface </a></li>

The panel that should collapse and expand:

<div class="collapse" ng-class="{in:showCreateNewInterfacePanel}"><div> 

the function that is fired

$scope.onAddInterface=function(){
  $scope.showCreateNewInterfacePanel=true;
}

anyway clicking the link nothing happens.

Am I missing something?

1
  • How could we know if you're missing something without seeing all of the code? Commented Jan 11, 2013 at 19:02

2 Answers 2

4

I'm not sure if this is how you are really defining your $scope.onAddInterface function or if it is just an example... Nevertheless you should be doing it like this:

$scope.onAddInterface = function() {
  $scope.showCreateNewInterfacePanel = true;
}

update

Also make sure the link and the collapsible element are under the same $scope/Controller:

<div ng-controller="Ctrl">
  ...
  <a ng-click="onAddInterface()">add interface</a>
  ...
  <div class="collapse" ng-class="{in:showCreateNewInterfacePanel}"><div>
  ...
</div>

Controller:

function Ctrl($scope) {
  $scope.onAddInterface = function() {
    $scope.showCreateNewInterfacePanel = true;
  }
}​
Sign up to request clarification or add additional context in comments.

4 Comments

Working fiddle,with only @bmleite's change. This fiddle shows that class "in" is being applied when you click. If it still doesn't work for you, then there may be something wrong with the CSS (not Angular).
Thanks for the reply but it still does not work, tried with simpler custom css and it still not working !!!!!!!!
Are sure the link is accessing the correct $scope? Does your onAddInterface function ever get called? Can you provide a bigger snippet from the HTML you are really using?
Yes, the problem was in the scope. would you mention it in your answer to accept it as the best answer ? the problem was that the collapsible element was out of the scope of the controller :)
4

I had a very similar problem and was able to solve this problem by placing 'in' in quotes. It is a string, and not a variable.

  ...
  <div class="collapse" ng-class="{'in':showCreateNewInterfacePanel}"><div>
  ...

Comments

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.