2014-01-13 125 views
0

目前我有這個jQuery了jQuery角獲得輸入字段值

$('body').on('click','.plot', function(){ 
     var y = $('input[name="gps[]"]').map(function() {return this.value;}).get(); 
     console.log(y); 
     console.log(y[0]); 
    }); 

我已經試過以下角度

theApp.controller('MenuSideController', ['$scope', function($scope) { 
    $scope.addnewmarker = function() { 
     var newdiv = document.createElement('div'); 
     newdiv.innerHTML = "<input type='text' name='gps[]' placeholder='Enter Address or Lat/Lon'>"; 
     document.getElementById('marker').appendChild(newdiv); 
     counter++; 
    }; 
    $scope.plotmarkers = function() { 
     var y = document.getElementsByName('gps[]').map(function() { 
      return this.value; 
      }).get(); 
     } 
     console.log(y); 
    }; 
}]); 

HTML

<div id="marker"> 
     <input type="text" name="gps[]" id="gps"> 
     <input type="text" name="gps[]"> 
    </div>       
<input type="button" value="Add another marker input" ng-click="addnewmarker();"></br> 

<button class="plot btn" ng-click="plotmarkers()">Plot</button> 

我想通過使用ng-click和I c來改變一個小的流程以在Angular中工作annot似乎得到這個工作,有人可以幫我嗎?

注:輸入獲得動態添加的,所以我可以永遠不知道有多少投入會有,我試圖做到這一點的角度,而不是jQuery的

回答

0

不要基於jQuery想,總以爲在建築術語。在jQuery中,您設計了一個頁面,然後使其成爲動態頁面。這意味着你可以使用jQuery直接修改DOM。但在角js創建您的自定義指令修改DOM。

爲了動態添加文本字段,我創建了一個控制器,如下所示。

app.controller('MainCtrl', function($scope) { 
    $scope.inputFields = []; 
    $scope.count = 0; 
    $scope.addField = function(){ 
    $scope.inputFields.push({name:"inputText"+$scope.count++}); 
    } 
}); 

有一個名爲addField()的函數。在div或按鈕等元素的單擊事件中調用此函數。檢查下面的html代碼。

<div ng-click="addField()">Click here to add</div> 
<div ng-repeat="inputField in inputFields"> 
    <input type="text" name="inputField.name"> 
</div> 

您必須閱讀這篇文章:How do i think in Angular if i have a jQuery background 。 檢查以下鏈接以瞭解添加表單域的想法。

  1. Create Html Elements dynamically
  2. Adding multiple input field
  3. Rendering form html dynamically
+0

我看到你說的話有,但我只是想如果那是可以從輸入提取數據? – ngplayground