2016-12-15 121 views
0

您好,我有一個奇怪的問題。我必須創建一個表單,但每個用戶的字段都不相同,因此他們可能是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'  
    ] 
}); 

但因爲我認爲這是行不通的。

謝謝你的幫助。

+1

應該工作的罰款。發佈後請檢查查看源 –

+0

什麼不行? – Carcigenicate

+0

可能無法在IE中工作http://stackoverflow.com/questions/2566394/changing-the-input-type-in​​-ie-with-javascript –

回答

2

您可以使用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

+0

它很棒!泰! – Daveus

0

你錯過了依賴注入的右方括號。這會工作。

app.controller('controller',['$scope', function($scope){ 
     $scope.prova=[ 
     'text', 
     'check-box'  
     ] 
}]); 
0

plunkr included

這對我的作品。模仿你的代碼,這裏有一個非常簡單的例子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> 
0

一些代碼的變化:

  • checkbox取代check-box
  • 用控制器功能代替controllermyCtrl

工作演示:

控制器:

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>