我是 Angular.js 的初学者。
我正在尝试通过从 mongodb
获取数据,在 angular.js
应用程序上生成动态表单。我已经通过 excel 表将导入的数据存储在 mongodb 上,所以这就是为什么所有 JSON
值都以 String
格式存储的原因。
为了解决这个问题,我通过检查 JSON 对象的值动态生成表单。
例如:如果值包含数字(“123456”),那么我将显示 input type="number"
,如果值包含电子邮件则 input type="email"
,值包含 dob 然后 datepicker
上..
以下是我的模板代码:
<div class="form-group" ng-repeat="(key,value) in providerList" ng-if="!$first">
<label>{{key.replace("_", " ") | uppercase}}</label>
<div ng-if="providerList[key].length > 100">
<textarea class="form-control" ng-model="providerList[key]"></textarea>
</div>
<div ng-if="providerList[key].length < 100 && !isNumeric(providerList[key]) && !checkEmail(providerList[key])">
<input type="text" class="form-control" id='datepicker' ng-model="providerList[key]">
</div>
<div ng-if="isNumeric(providerList[key])">
<input type="number" class="form-control" ng-model="providerList[key]">
</div>
<div ng-if="checkEmail(providerList[key])">
<input type="email" class="form-control" ng-model="providerList[key]">
</div>
</div>
这是我的控制器:
$scope.isNumeric = function (data) {
if (isNaN(data)) {
return false;
} else {
return true;
}
// if (/^\d+$/.test(data)) {
// return true;
// } else {
// return false;
// }
};
$scope.checkEmail = function (email) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
};
每当调用 isNumeric
函数时,它都会在控制台上显示以下错误:
Error: [ngModel:numfmt] Expected `1` to be a number
http://errors.angularjs.org/1.4.8/ngModel/numfmt?p0=1
minErr/<@http://localhost:1000/js/vendor/angular/angular.js:68:12
numberInputType/<@http://localhost:1000/js/vendor/angular/angular.js:21977:1
ngModelWatch@http://localhost:1000/js/vendor/angular/angular.js:25489:21
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:1000/js/vendor/angular/angular.js:15888:34
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:1000/js/vendor/angular/angular.js:16160:13
done@http://localhost:1000/js/vendor/angular/angular.js:10589:36
completeRequest@http://localhost:1000/js/vendor/angular/angular.js:10787:7
requestLoaded@http://localhost:1000/js/vendor/angular/angular.js:10728:1
return logFn.apply(console, args);
angular.js (line 12520)
Error: [ngModel:numfmt] Expected `1234567890` to be a number
http://errors.angularjs.org/1.4.8/ngModel/numfmt?p0=1234567890
minErr/<@http://localhost:1000/js/vendor/angular/angular.js:68:12
numberInputType/<@http://localhost:1000/js/vendor/angular/angular.js:21977:1
ngModelWatch@http://localhost:1000/js/vendor/angular/angular.js:25489:21
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:1000/js/vendor/angular/angular.js:15888:34
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:1000/js/vendor/angular/angular.js:16160:13
done@http://localhost:1000/js/vendor/angular/angular.js:10589:36
completeRequest@http://localhost:1000/js/vendor/angular/angular.js:10787:7
同样,我正在调用电子邮件检查功能 checkEmail
并且它工作得很好,所以 isNumeric
功能有什么问题。
原文由 J.K.A. 发布,翻译遵循 CC BY-SA 4.0 许可协议
然后,在各自的输入上使用“字符串到数字”。
参考: https ://docs.angularjs.org/error/ngModel/numfmt