-2

Simple ng-class binding is not triggered when called inside a function, how can I resolve this?

$scope.afterHide = function() {
  $scope.inputState = "fade-in"; 
}
<label ng-class="inputState">Next statement</label>

3
  • Inside scope options 'my-show': '=' should be 'myShow': '=' Commented Jun 14, 2017 at 16:21
  • Yes.. there's actually a lot of error with setting up that directive, but its not relevant to my question. The actual error is in : $scope.afterHide = function() { $scope.inputState = "fade-in"; // this line is not called here WHY? console.log('Refresh statement fully hidden AFTERHIDE'); } Commented Jun 14, 2017 at 16:33
  • Eliminate any issues that aren't relevant to the problem. See How to create a Minimal, Complete, and Verifiable example. Commented Jun 14, 2017 at 17:20

1 Answer 1

1

Works fine in this example:

angular.module("app",[])

.controller("ctrl", function($scope) {

  $scope.afterHide = function() {
      $scope.inputState = "fade-in"; 
  };
  
  $scope.reset = function() {
      $scope.inputState = "";
  };
});
.fade-in {
    background-color: red;
}
<script src="//unpkg.com/angular/angular.js"></script>
<div ng-app="app" ng-controller="ctrl">

  <label ng-class="inputState">Next statement</label>

  <br><button ng-click="afterHide()">Invoke afterHide</button><br>
  <button ng-click="reset()">Reset</button>
</div>


If it works then I think it has something to do with the overall logic of my code. The function $scope.afterHide is triggered by an event on one of the directives, this directive is defined outside the controller. In html its basically just another div that has a state of change. When a change happens, other elements on the page will also be affected, in this context, that other element is the label tag. When the change happens, the $scope.afterHide function within the controller is called by the directive which is defined outside of the controller.

Scopes are arranged in hierarchical structure which mimic the DOM structure of the application.

The ng-click directive cannot invoke a function on a scope that is outside its hierachy. Also if the ng-click directive is on a template inside a directive that uses isolate scope, the event must be connected to parent scope with expression, "&", binding.

For more infomation, see

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for replying. If it works then I think it has something to do with the overall logic of my code. The function $scope.afterHide is triggered by an event on one of the directives, this directive is defined outside the controller. In html its basically just another div that has a state of change. When a change happens, other elements on the page will also be affected, in this context, that other element is the label tag. When the change happens, the $scope.afterHide function within the controller is called by the directive which is defined outside of the controller.
The directive was calling the function, they both had the same controllers but they were out of scope. So I just rearranged the tags in the html file and got it working . I'm trying to give you a point but I don't have enough privilege to do that yet

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.