Vandervals
Vandervals

Reputation: 6054

Revert ng-model value in Angular JS

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?

Upvotes: 0

Views: 479

Answers (2)

Sim
Sim

Reputation: 3317

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

Upvotes: 1

Stev_k
Stev_k

Reputation: 2126

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.

Upvotes: 0

Related Questions