Angular JS - 模型不更新数字输入字段

时间:2014-01-15 22:52:42

标签: javascript html angularjs

我有以下内容,我确信这里有一些简单的解决方案,我只是在俯视。我正在将数据加载到模型中,但它没有更新输入字段。

<div ng-app>
  <h2>Testing</h2>
  <div ng-controller="MyCtrl">
  From: <input name="Price" type="number" ng-model='object.number["From"]' /> 
  To: <input name="Price" type="number" ng-model='object.number["To"]' />
  </div>
</div>

和JavaScript:

function MyCtrl($scope) {
    $scope.object = {number : {}};
    $scope.object['number'] = {From: null, To: null}
    console.log($scope.object['number']);

    $scope.loadPrice = function(){
        $scope.object['number'].From = "5";   
        $scope.object['number'].To = "5";   
    }

    $scope.loadPrice();
    console.log($scope.object['number'])

}

包括小提琴:

http://jsfiddle.net/pCnfH/6/

1 个答案:

答案 0 :(得分:5)

您将输入约束为type="number",但将值设置为字符串:$scope.object['number'].To = "5";

因此,您要么将值设置为数字:

$scope.object['number'].From = 5;   
$scope.object['number'].To = 5;   

updated fiddle

或删除数字约束:that fiddle