2016-02-13 49 views
1

为什么角度模型不适用于输入类型编号?但在输入类型文本中起作用。html5 input type =“number”在angularjs中不适用于ng-model =“selectedItem.price”

这里是我的代码

<input type="number" step="0.01" name="price" ng-model="selectedItem.price" autocomplete="off"> 

这里的解决方案。在我的数据库中的数据类型是小数不串..但angularjs使其串它得到的数据之后..所以我加上“+”将其再次转换为int。

$scope.selectedItem={ item_id:"", item_name:"",supplier:"",price:"",description:"" }; 

<input type="number" step="0.01" name="price" ng-model="+selectedItem.price" autocomplete="off"> 
+0

这是什么**步骤**? ** ng-model **中传递的值是多少? – Thangadurai

+0

在输入元素 – Jayrex

+1

中设置十进制格式在ng-model中传递什么值?我认为在** selectedItem.price **你的传递字符串...你可以检查ng-model的数据类型..如果你传递** selectedItem.price ==“2”**这样它不会工作。只需要传递整数值。 – Thangadurai

回答

0

检查这个代码

<html lang="en"> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Example - example-number-input-directive-production</title> 
     <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> 
    </head> 
    <body ng-app="numberExample"> 
     <script> 
      angular.module('numberExample', []) 
        .controller('ExampleController', ['$scope', function($scope) {  
        $scope.selectedItem = { 
         price: 2.5 
        }; 
       }]); 
     </script> 
     <form name="myForm" ng-controller="ExampleController"> 

      <input type="number" step="0.01" name="price" ng-model="selectedItem.price" autocomplete="off"> 
      <tt>value = {{selectedItem.price}}</tt><br/> 
     </form> 
    </body> 
</html> 
+0

但它不会在输入元素 – Jayrex

+0

中显示数值,您是否使用单个文件来测试我的代码。 ?? 我已经创造了新鲜的档案。和它的工作.. –

+0

你可能会错过一些控制器名称或东西,没有检查你的JS代码,我们不能判断,这就是为什么我创建了一个新的文件,并告诉你,你的HTML部分是正确的,但问题可能在那里在你的JS代码或语法。 –

0

我觉得在selectedItem.price乌尔通过串...您可以检查数据类型NG-模型..如果乌拉圭回合传球

$scope.selectedItem={ 
    price="2" 
}; 

像上面它不会工作。只需要传递整数值。像下面的意思,它会起作用。

$scope.selectedItem={ 
price:2; 
} 

将字符串转换为整数。

+0

这是我的... $ scope.selectedProduct = {product_id:“”,product_name:“”,supplier:“”,price:“”,description:“”};如果我删除了引用的代码将无法正常工作,所以我做的是,我只是把“+”放在selectedItem.price中转换为int。 – Jayrex

相关问题