我正在尝试在 Controller 中检索输入[number]。
我找到了一个解决方案,它说在输入上添加 $scope.$watch 。
我尝试过,但现在我的号码是“NaN”。
<div class="offerText">
<label class="form-inline" ng-if="!admin">What will it cost ? <input type="number" class="form-control" ng-model="estimation"/></label>
<label class="form-inline">Comments <textarea rows="3" cols="50" class="form-control" ng-model="comments"></textarea></label>
</div>
在我的 Controller 中
$scope.$watch('estimation', function (val, old) {
$scope.estimation = parseFloat(val);
});
感谢您的帮助!
最佳答案
你不需要 watch 。这里的问题是输入位于 ng-if 指令内,该指令定义了自己的范围。估计值存储在 ng-if 范围中,而不是存储在 Controller 范围中。
规则或经验:您的 ng-model 中始终有一个点。
在您的 Controller 中,添加
$scope.data = {};
并在 html 中使用
ng-model="data.estimation"
和
ng-model="data.comments"
关于带数字输入的 Angularjs ng-model,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29280105/