![](/img/trans.png)
[英]change input type of contentEditable cell to “number” dynamically
[英]Change the type input dynamically
您好,我有一個奇怪的問題。 我必須創建一個表單,但每個用戶的字段都不同,因此它們可以是a或。 我試圖用角做這樣的事情
<div ng-controller="myCtrl" ng-repeat="x in prova">
<input type = "{{x}}">
</div>
還有像這樣的控制器
app.controller('controller',['$scope', function($scope){
$scope.prova = [
'text',
'check-box'
]
});
但正如我認為的那樣,它不起作用。
謝謝您的幫助。
您可以使用ng-switch
指令僅在其類型與x匹配時呈現正確的輸入類型。
<div ng-controller="myCtrl" ng-repeat="x in prova" ng-switch="x">
<input type="text" ng-switch-when="text">
<input type="checkbox" ng-switch-when="check-box">
<textarea ng-switch-when="textarea"></textarea>
</div>
更多信息: https : //docs.angularjs.org/api/ng/directive/ngSwitch
您錯過了依賴項注入的右方括號。 這會起作用。
app.controller('controller',['$scope', function($scope){
$scope.prova=[
'text',
'check-box'
]
}]);
這個對我有用。 這是一個在plunkr中非常簡單的示例,它模仿您的代碼。
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.arrTypes = ['submit', 'text', 'button', 'password', 'month'];
});
並在index.html ...中:
<div ng-repeat = "inpType in arrTypes">
<input type={{inpType}} name="" id="" value={{inpType}} />
</div>
一些代碼更改:
check-box
以checkbox
。 controller
與myCtrl
在控制器的功能。 工作演示:
控制器:
var myApp = angular.module('myApp',[]);
myApp.controller('myCtrl', function($scope) {
$scope.prova = [
"text",
"checkbox"
]
});
查看:
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="x in prova">
<input type = "{{x}}">
</div>
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.