0

Is it possible to revert the model value when displaying it?

My controller has this property:

this.config = {client: false, name: true};

And I want to use the values like this:

<label>
  <input ng-model="ctrl.config.client"> Client
</label>

<div ng-hide="ctrl.config.client">Client</div>

I'd like to show the input checked when the config.client value is false. How can I do it?

2
  • 1
    Can you expand your example? I have no idea what you are trying to accomplish. Commented Sep 30, 2015 at 15:59
  • Do you want to change the checked state of an input type="radio" or "checkbox"? Commented Sep 30, 2015 at 15:59

2 Answers 2

1

UPDATE: If you want to check a checkbox, and you use $scope you can use ng-true-value="false" and ng-false-value="true" to revert the default values. This works only, if you use $scope instead of this! Here is an example:

<body ng-app="app">
  <div ng-controller="ctrl">
  <label>
    <input type="checkbox" ng-model="config.client" ng-true-value="false" ng-false-value="true">
  </label>

  <div ng-hide="config.client">Client</div>
  </div>
<script>
  angular.module('app', []).controller('ctrl', function($scope) {
    $scope.config = {client: false, name: true};
  });
</script>
</body>

And the plunker to try it: http://plnkr.co/edit/vRIZMb2CpDQAKHu91yhN?p=preview

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

2 Comments

But that blocked the hability to unchek it, I don't know why!
You're right, I've updated my post, please try it again.
0

If I understand correctly I think you want ng-checked.

And then negate it. So something like:

<label>
 <input type="checkbox" ng-checked="!ctrl.config.client" ng-model="ctrl.config.client"> Client
</label>

I am assuming you want a checkbox although it isn't clear from your example.

1 Comment

this creates a weird behaviour, you have to click twice to change state, or something like that.

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.