我会推荐更通用的方法。如果你想按类型区分您输入首先你需要声明某种类型的检查功能在你的控制器:
$scope.getType = function(x){
return Object.prototype.toString.call(x);
}
你必须这样做,因为这是不可能做到这一点的表达 - 见:How to get a type of scope variable in Angular expression?
然后在您的视图中,您可以使用ng-if指令根据字段的类型显示不同的控件。这是布尔表达式示例:
ng-if="getType(parameter_list[$index].value) == '[object Boolean]'"
你也应该正确地定义你的布尔值,而不是“真” /“假”,但真/假:
{
"title": "differed",
"value": true
}
最后你的例子的代码看起来如下。
查看:
<div ng-controller="MainCtrl" class="container">
<div>
<table class="table table-hover table-striped table-bordered">
<tbody>
<tr ng-repeat="parameter in parameter_list">
<th class="text-left">
{{parameter.title | uppercase}}
</th>
<td class="text-left">
<div class="form-group" ng-if="getType(parameter_list[$index].value) != '[object Boolean]'">
<input type="text" ng-model="parameter_list[$index].value" class="form-control">
</div>
<div class="form-group checkbox" ng-if="getType(parameter_list[$index].value) == '[object Boolean]'">
<label><input type="checkbox" ng-model="parameter_list[$index].value">{{ parameter_list[$index].title }} </label>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
控制器:
var mymodal = angular.module('mymodal', []);
mymodal.controller('MainCtrl', function ($scope) {
$scope.getType = function(x){
return Object.prototype.toString.call(x);
}
$scope.parameter_list = [
{
"title": "name",
"value": "Product3"
},
{
"title": "version",
"value": "01.00.00"
},
{
"title": "inventory_name",
"value": "Product3"
},
{
"title": "inventory_version",
"value": "01.00.00"
},
{
"title": "differed",
"value": true
},
{
"title": "differed_name",
"value": "whatever"
},
{
"title": "accept_error_while_reboot",
"value": false
},
{
"title": "setup",
"value": ""
},
{
"title": "ggg",
"value": "setup.exe"
},
{
"title": "fx",
"value": "test"
},
{
"title": "gx",
"value": "setup.exe"
},
{
"title": "tx",
"value": "setup.exe"
}
]
});
在这里你可以找到的jsfiddle:http://jsfiddle.net/57qhsqwf/2/
您可能需要传达此问题“这样的问题是,我没有$ scope变量中的输入类型。”一些在你的帖子从哪里保存重复问题肯定 – Prasad