2017-09-20 32 views
1

在html文件中更新了輸入字段的值。使用$ scope,第一次我可以分配輸入字段。但是,當我改變輸入字段在htlm中,輸入字段值改變,但在js中,控制檯日誌值沒有改變。我想獲得js函數中的更新值。而且我使用的是laravel框架。在實踐中它是有效的。但我的項目不起作用。謝謝。雙向數據綁定不適用於我

的index.html

<div class="form-group"> 
<div class="col-sm-12"> 
    <input type="text" class="form-control" 
      id="send_amount" 
      name="send_amount" 
      ng-model="send_amount" 
      ng-keyup="calculateReceivedAmount()" required> 
</div> 
@{{ send_amount }} 

而我的JS文件

var exchange = angular.module('app', []); 
exchange.controller('MoneyExchangeController', MoneyExchangeController); 
function MoneyExchangeController($scope, $http) { 
    $scope.send_amount = 100; 
    $scope.calculateReceivedAmount = function() { 
     console.log($scope.send_amount); 
    } 
} 
+0

我試着撥弄你的代碼,它看起來就像是我 – Rahul

回答

2

AngularJS具有約束力的非對象變量的問題,嘗試結合您的輸入如下:

index.html

<input type="text" 
     class="form-control" 
     id="send_amount" 
     name="send_amount" 
     ng-model="send_amount.value" 
     ng-keyup="calculateReceivedAmount()" 
     required /> 

app.js

var exchange = angular.module('app', []); 
exchange.controller('MoneyExchangeController', MoneyExchangeController); 
function MoneyExchangeController($scope, $http) { 
    $scope.send_amount = { 
     value: 100 
    }; 
    $scope.calculateReceivedAmount = function() { 
     console.log($scope.send_amount.value); 
    }; 
}  
+0

工作的工作!你太棒了。非常感謝。 –

+1

我的榮幸,很高興它的工作。 –