martinwang1985
martinwang1985

Reputation: 556

Angular: ng-model=value *0.5, possible?

My code is like that:

I want that, while user modify the value of testVaalue in the first input, the second input changed automatically. Is it possible?

Thank you very much

Upvotes: 1

Views: 2768

Answers (2)

adam0101
adam0101

Reputation: 31005

No, you can't do it like that, but should be able to do:

<input type="text" readonly value="{{(value * 0.5) || 0}}" />

Or you could just display the result as text:

{{(value * 0.5) || 0}}

Upvotes: 1

shri
shri

Reputation: 63

I would suggest declaring a new variable which can track half value.

Example

Template

<div ng-controller="MyCtrl">
  value:{{value}} half:{{value*0.5}}!
  <br>
  <input ng-model="value" type="text">
  <input ng-model="half" type="text">
</div>

controller code

var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {
    $scope.value = 5;
    $scope.half = $scope.value * 0.5;

    $scope.$watch('value', function() {
        $scope.half = $scope.value * 0.5;
    });
}

Upvotes: 1

Related Questions