5

我想做一個簡單的例子,其中每次用戶點擊按鈕時輸入字段和按鈕字段。如何在按鈕點擊的angularjs中動態添加輸入字段?

當點擊與輸入字段一起出現的新按鈕時,如何獲取輸入字段的文本?

http://codepen.io/anon/pen/yNLGzx

var app = angular.module('ionicApp',['ionic']); 
app.controller('cntr',function($scope){ 
    $scope.addfield=function(){ 
     alert("how to add input field dyanmically") 
    } 

}) 

我不知道如何做到這一點;在jQuery中我們可以使用append功能,就像這樣

$('.addcontend').append('<input \> <button>get input value</button>') 

我該如何使用angularjs實現這個功能?

回答

11

你可以做的是通過數組使用數組來表示輸入,則循環,並使其像

<div class="addcontend"> 
    <div ng-repeat="item in inputs"> 
    <input ng-model="item.value"\> <button>get input value</button> 
    </div> 
    </div> 

然後

$scope.inputs = []; 
    $scope.addfield=function(){ 
    $scope.inputs.push({}) 
    } 

演示:CodePen

+0

感謝答案但如何獲得從動態生成的按鈕動態生成的輸入字段的值 – user944513

+0

您想要什麼值? –

+0

動態按鈕點擊 – user944513

相關問題