0

I m trying to do a workaround for a bug. i need to just change the css of an element when an other checkbox is clicked. But it is not working.. It just works when i click on an other button somewhere else but when i click on the checkbox the view is not being refreshed maybe ? Any ideas ?

View:

<input
    type="checkbox"  
    value="application.callback" id="telefonBox"
    ng-click="application.callback = !application.callback; toggleClass(application.callback)"
/>

Controller:

$scope.toggleClass = function(newValue) {
    var element = angular.element(document.querySelector('#additional'));

    if (newValue) {
        element.toggleClass("tooltip-agent tooltip-agentChecked ");
    } else {
        element.toggleClass("tooltip-agentChecked tooltip-agent");
    }
    $scope.$apply();
}

i tried this to but not working

$scope.$watch('$scope.application.callback', function (newValue, oldValue) {
    var element = angular.element(document.querySelector('#additional'));
    if (newValue) {
        element.toggleClass("tooltip-agent tooltip-agentChecked ");
    } else {
        element.toggleClass("tooltip-agentChecked tooltip-agent ");
    }
1
  • add more code, esp the additional part Commented Feb 26, 2016 at 9:50

3 Answers 3

1

add ng-modal into checkbox

<input type="checkbox" ng-model="application.callback">

and ng-class into your #additional element

<div id="additional" ng-class="{true:'tooltip-agent tooltip-agentChecked', false:'tooltip-agentChecked tooltip-agent'}[application.callback]"></div>
Sign up to request clarification or add additional context in comments.

Comments

1

DEMO

You should not manipulate elements in angular as much as possible, you can do it easier with ng-class like this

<div id="test" ng-class='{ active: vm.isChecked }'>
  lorem
</div>

ng-class accepts an object as parameter, in this case it's { active: vm.isChecked } which mean if vm.isChecked evaluates to true, the active class will be applied to the element

Comments

0
  $scope.selection = function($event) {
  var checkbox = $event.target;
  var action = (checkbox.checked ? 'check' : 'uncheck');
    if(action == "check")
       angular.element(document.querySelector('#additional')).addClass("tooltip-agent tooltip-agentChecked");
    else
       angular.element(document.querySelector('#additional')).addClass("tooltip-agentChecked tooltip-agen");
};
  });


  <input type="checkbox" id="additional" ng-model="check" ng-click="selection($event)"  >

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.