2017-04-12 65 views
-3

在這裏,我想在控制器功能evrytime文本字段值我改變了文本字段如何使用ng-change獲取控制器中的當前文本字段值?

<input type="text" name="quantity" ng-model="viewItemData1.quantity" ng-change="changePrice($event.target.value);"> 

$scope.changePrice = function(val) 
    { 
    console.log(val); 
    alert(val); 
    alert(JSON.stringfy(console.log(val))); 


    } 

回答

0

在控制器中定義一個變量

$scope.val; 

然後在你的HTML中使用NG-模型

<input id="name" ng-model="val"> 

在這種情況下,您可以簡單地:

<input type="text" name="quantity" ng-model="viewItemData1.quantity" ng-change="changePrice();"> 

$scope.changePrice = function() 
{ 
console.log($scope.viewItemData1.quantity); 
alert($scope.viewItemData1.quantity); 
alert(JSON.stringfy(console.log($scope.viewItemData1.quantity))); 
} 
+0

剛纔固定的,做工精細 $ scope.changePrice =功能(VAL) \t { \t \t \t var tprice =(val * $ scope.viewItemData1.quantity); $ scope.viewItemData.tprice = tprice; \t} – Chinna

1

在HTML ng-model="viewItemData1.quantity"將數據綁定護理,你不需要ng-change方法,所以在HTML:

<input type="text" name="quantity" ng-model="viewItemData1.quantity"> 

然後在你的控制器,設置一個$手錶戴在輸入字段做任何你想這樣做時,它的變化:

$scope.$watch("viewItemData1.quantity", function(newVal, oldVal) { 
    if (newVal !== oldVal) { 
     console.log(newVal); 
     alert(newVal); 
     // or do whatever you want to do. 
    } 
}); 
相關問題