How to dynamically change input value

后端 未结 1 1391
轮回少年
轮回少年 2021-01-20 19:19

I\'m trying to show some editable results to the users, so I show them through an input field. This is a basic example of what I\'m doing:

1条回答
  •  温柔的废话
    2021-01-20 19:29

    What do you want to happen when the user edits the results input? Do you want the data binding to break (I assume not and ignore this possibility)? Do you want one of the inputs to adjust to the proper value?

    If you only want to display the output, do this, in your controller:

    $scope.result = function() { return $scope.first + $scope.second; }
    

    And in your view:

    {{ result() }}
    

    But if you want the user to be able to edit the result and (let's say) have second be assigned (result - first), then you'd want something like this in your view (by the way, note the type="number"):

    
    
    
    

    And in your controller:

    $scope.adjustResult = function() {
      $scope.result = $scope.first + $scope.second;
    };
    $scope.adjustResult(); // initialize result
    
    $scope.adjustInput = function() {
      $scope.second = $scope.result - $scope.first;
    }
    

    0 讨论(0)
提交回复
热议问题