2017-03-16 41 views
0

我想根據數據列表計數填充文本框字段。如果計數是3,我必須填充3本教科書。基於數據列表值填充角度js中的文本字段

<datalist id="testList"> 
    <option ng-repeat="des in DesList" value=" {{des.id}} ({{des.code}})" data-ng-change="addText(des.id)> 
</datalist> 

我嘗試創建像這樣的腳本,但沒有運氣。

this.addText = function(count) { 

    this.arrayText.push(obj); 

    } 
} 
+0

添加更多的信息 –

+0

你在哪裏定義obj? –

回答

0

嘗試使用此Jsfiddle demo

這裏

更新代碼Jsfiddle link updated

JS代碼

var app = angular.module('myApp', []); 
    app.controller('ctrl1', function($scope) { 
    $scope.rules = [{ 
     name: 'rule1', 
     id: 1 
    }, { 
     name: 'rule2', 
     id: 2 
    }]; 
    $scope.data = []; 
    var dummy = []; 

    $scope.addText = function(rule) { 

     dummy.push(rule); 
     if (dummy.length > 2) { 
     $scope.data = angular.copy(dummy); 
     $scope.one = $scope.data[0]; 
     $scope.two = $scope.data[1]; 
     $scope.three = $scope.data[2]; 
     } 
    } 
    }); 

HTML代碼

<div ng-app='myApp'> 
    <div ng-controller='ctrl1'> 
     <select ng-options='item.name as item.name for item in rules' ng-model='rule' ng-change='addText(rule)'> 

     </select> 
     <hr> Length: {{data.length}} 
     <hr> 
     <div ng-if='data.length > 2'> 
     <input type='text' ng-model='one'> 
     <input type='text' ng-model='two'> 
     <input type='text' ng-model='three'> 
     </div> 
    </div> 
    </div> 

根據你的問題,我建立這個,但你可以擴大此

+0

你的答案的一部分是寫。如果選擇的索引是2,如何刪除一個文本框。請幫助 – sanjeewa

+0

但是,我如何添加文本框等於選定的值。如果選擇的值是1,我想顯示1個文本框。如果選擇值4,我想顯示4個文本框。請幫忙 – sanjeewa

+0

結帳這一個https://jsfiddle.net/7ym139q9/4/ –

相關問題