簡體   English   中英

angular-將綁定到元素的ngmodel傳遞給綁定到另一個元素的函數

[英]angular - passing ngmodel bound to an element to a function bound to another element

我需要將在input[type="text"]元素中input[type="text"]的值傳遞給綁定到另一個元素的函數calculate() 因此,無論何時輸入字段中的值發生變化,計算得出的值都應反映在Result

如果可以通過其他任何方式完成,請告訴我。 感激。

我的HTML如下:

<tr>
    <td>Points</td>
    <td ng-repeat="score in player.scores ">
       <input type="text" ng-value="{{score.stroke}}" ng-model="???????"/>
    </td>
</tr>
<tr>
    <td>Result</td>
    <td ng-repeat="param in cellParams >{{calculate(??????)}}</td>
</tr>

使用ng-model時,您不需要ng-value 。所以我想這可以工作:

angular.module('app', []).controller('controller', function($scope) {
    $scope.calculate = function() {
    return $scope.result = parseInt($scope.stroke) + 1;
  }
});

JsFiddle

您可以使用ng-keyup 每當您更改輸入時,都會影響您的結果。

有關ng-keyup的示例:

在html中:

<div ng-app="app" ng-controller="ctrl">
    <input type="text" ng-keyup="cal()" ng-model="num">
    <div>{{ result }}</div>
</div>

在控制器中:

angular.module('app', []).controller('ctrl', function($scope){
    $scope.cal = function()
    {
        return $scope.result = $scope.num + 1;
    }
});

代碼

希望對您有所幫助。

我已經使用$ watch()實現了。

$scope.$watch('player', function(newVal, oldVal) { 
      calculate(newValue);
} 

從Alpesh的提示中得到了這個答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM