我會推薦更通用的方法。如果你想按類型區分您輸入首先你需要聲明某種類型的檢查功能在你的控制器:
$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