2017-04-12 34 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. 
    } 
}); 
相关问题