2016-05-05 5 views
1
<div class="info-block" ng-app=""> 
          <div ng-controller="Note"> 
          <div class="checkbox"> 
           <label> 
            <p><b>Primary Publication: </b> 
            {{ form_widget(form.input_ppubs, { 'attr': {'class': 'valOption'}}) }} 

            </p> 
           </label> 
          </div> 

           <div ng-repeat="item in items"> 
            <input type="text" placeholder="new input" ng-model="item.primaryPub"> 
            </div> 
           <button type="button" ng-click="add()">New Item</button> 
          </div> 

angularjsが私はHTMLの入力フィールドの値を取得し、ボタンにより入力からそれを削除しようとしています

私は、コードを取得することができています

をクリックし使用して入力フィールドの値を削除する方法、コードを削除する方法はわかりません。

var Note = function($scope){ 
      $scope.items = []; 

     $scope.add = function() { 


      //angular way of implementing document.getElementByID(); 
      pub1 = angular.element('#form_input_ppubs').val(); 



      $scope.items.push({ 
      primaryPub: pub1 

      }); 
     }; 
     } 

答えて

0

そうした場合:

console.log($scope.items); 

あなたはprimaryPub、あなたの入力のためのモデルのエントリが表示されるはずです。そして、あなたはそう、モデルをゼロにすることによって、それをターゲットにすることができます:

$scope.items.primaryPub = null; 

あなたはNGリピート内でこれを使用しているしかし:あなたが複数ある場合(

<div ng-repeat="(i, item) in items"> 
    <input type="text" placeholder="new input" ng-model="items[i].primaryPub"> 
</div> 

だからあなたはconsole.log 'items'内のitem)は、primaryPubの配列のような構造を示すはずです。

3

このようなアイテムを取得する必要はありません。それは醜いもので、角度のないものです。 angular.element('#form_input_ppubs').val();

代わりに、ngModelを入力してください。

あなたのスコープ内に宣言してください。

$scope.inputItem = null; 

HTML

<input ng-model="inputItem " /> 

あなたの機能でそれを使用してください:

​​3210

を使用して、それを呼び出しNGをクリック

<button type="button" ng-click="addItem(inputItem)">Add Item</button> 
+0

[ここ](https://jsfiddle.net/awolf2904/sp2fq0h1/)は、ng-modelでの入力フィールドのクリアを示すデモです。 – AWolf

+0

私はhtmlページを生成するためにTwig(Symfony)を使用しているため、ng-model属性を作成できません。 @KKKKKKKKK –

関連する問題