我想要动态设置input
字段。但我想出了error
。如何使用ng-switch-when
在input
字段或任何其他字段中设置类型?
这是我的温度:
var GridTemplate = [
'<div>',
'<div>',
'<ul class="titles"><li ng-repeat="page in currentPage">{{page.title}}</li></ul>',
'<div class="rowContent">',
'<ul ng-repeat="(title,page) in currentPage">',
'<li ng-repeat="(key,element) in page.key track by $index">',
'<input ng-switch-when="page.key[key] == string" type="text" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />',
'<input ng-switch-when="page.key[key] == number" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />',
'</li></ul></div>',
'<div class="pageNavigator"><ul><li ng-repeat="page in slides"><a ng-href="">{{$index+1}}</a></li></ul></div>',
'</div>',
'</div>'
];
答案 0 :(得分:1)
试试这个:
<input type="{{getInputType(page.key[key])}}" ng-model="page.key[key]" />
然后控制器中的一个函数检查参数并返回输入类型
$scope.getInputType = function(param){
if(angular.isString(param)) return "text";
if(angular.isNumber(param)) return "number";
}
答案 1 :(得分:0)
ng-switch-when
指令需要包含在ng-switch
指令中。
来自here:
<ANY ng-switch="expression">
<ANY ng-switch-when="matchValue1">...</ANY>
<ANY ng-switch-when="matchValue2">...</ANY>
<ANY ng-switch-default>...</ANY>
</ANY>
所以在你的情况下,我认为它会像是
<li ng-switch="page.key[key]" ng-repeat="(key,element) in page.key track by $index">
<input ng-switch-when="string" type="text" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />
<input ng-switch-when="number" name="" id="" ng-model="page.key[key]" ng-blur="numSort( page )" />
</li>