2016-02-13 25 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

相關問題