1

If vm.agentCommission is 0 then I want the field to be blank, so the user dont have to remove the zero before typing.

I tried this:

 <input ng-model="vm.agentCommission ? '' : vm.agentCommission" ....

But I get this:

Expression 'vm.agentCommission ? '' : vm.agentCommission' is non-assignable.

Is there a way to do this in angular?

torbenrudgaard
  • 2,375
  • 7
  • 32
  • 53

2 Answers2

0

use the ternary operation inside ng-change

ng-change="(vm.agentCommission === '0') ? vm.agentCommission = '' : vm.agentCommission"

angular.module("app",[])
.controller("ctrl",function($scope){
 var vm = this;
 //vm.agentCommission = "";

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl as vm">
<input ng-model="vm.agentCommission" ng-change="(vm.agentCommission === '0') ? vm.agentCommission = '' : vm.agentCommission"> 
</div>
Sachila Ranawaka
  • 39,756
  • 7
  • 56
  • 80
  • What is the "===" ? – torbenrudgaard Jul 24 '17 at 12:55
  • check this https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons – Sachila Ranawaka Jul 24 '17 at 12:56
  • Ok thanks. Your solution only works if I set it to "" in the controller and I wanted to avoid doing that. And even if I do that, change it to 1 and then back to 0, it does not blank the field. – torbenrudgaard Jul 24 '17 at 12:57
  • what do u mean? i remove the controller part and it works – Sachila Ranawaka Jul 24 '17 at 13:00
  • Ahh yes it works when you make changes to the field, you are no longer allowed to type "0" as the value. But what about when the field is initialized with a 0 in `vm.agentCommission` - then the zero is still there. How do I get rid of that? Is the controller the only way? – torbenrudgaard Jul 24 '17 at 13:06
  • I tried this, it does not change the 0 with blank. `ng-init="(vm.agentCommission === '0') ? vm.agentCommission = '' : vm.agentCommission"` – torbenrudgaard Jul 24 '17 at 13:11
  • got it... ` ng-init="(vm.agentCommission == 0) ? vm.agentCommission = '' : vm.agentCommission"` this worked – torbenrudgaard Jul 24 '17 at 14:12
0
<input ng-model="vm.agentCommission > 0 ? vm.agentCommission  : '' " >

I changed your code bit. Kindly check if this works.