1

WebサービスからJSONで作成したテーブルを表示しようとしていて、配列を作成しています。配列の長さは0です。ボタンをクリックすると、配列に1要素を追加してテーブルに新しい行を追加します。ボタンをクリックしても、配列の要素が追加されず、その理由がわかりません。同じコントローラとビューで2つの異なるng-repeatを持つことは可能ですか?単一のビューで複数のng-repeatを使用する

コントローラがあります:

assets.controller('EditaTipusCtrl', function ($scope, $http, $routeParams) { 
       $http.get('http://10.0.203.73/WS/ws.php/tipusactius/getDetails/' + $routeParams.id).success(function (data) { 
        $scope.atrb = data; 
       }); 

       $scope.nousAtributs = []; 

       $scope.addNewLine = function() { 
        var newRow = { 
         "nomAtribut": "", 
         "tipus": "", 
         "mida": '', 
         "prioritat": "", 
         "obligatori": "", 
         "observacions": "", 
         "nomTipusActiu": $routeParams.id // nom del tipus d'actiu 
        }; 
        $scope.nousAtributs.push(newRow); 
       //  return $scope.nousAtributs; 
       } 

      }); 

とビュー:

解決
<div class="col-md-10"> 
    <div ng-controller="EditaTipusCtrl" id="test"> 
     <div class="row"> 
      <button class="btn btn-default" > 
       <span class="glyphicon glyphicon-plus" aria-hidden="true" ng-click="addNewLine()"></span> 
       Afegir atribut 
      </button> 
     </div> 
     <br /> 
     <table class="table"> 
      <tr> 
       <th>#</th> 
       <th><a href="" ng-click="sortField = 'ordre'; reverse = !reverse">Prioritat</a></th> 
       <th><a href="" ng-click="sortField = 'nomAtribut'; reverse = !reverse">Atribut</a></th> 
       <th><a href="" ng-click="sortField = 'midaAtribut'; reverse = !reverse">Mida</a></th> 
       <th><a href="" ng-click="sortField = 'atributObligatori'; reverse = !reverse">Obligatori</a></th> 
       <th><a href="" ng-click="sortField = 'observacions'; reverse = !reverse">Observacions</a></th> 
      </tr> 
      <tr ng-repeat="(key, value) in atrb"> 
       <td></td> 
       <td> 
        <input type="number" ng-model="value.ordre" value="value.ordre" /> 
       </td> 
       <td> 
        <input type="value.valor" ng-model="value.nomAtribut" value="value.nomAtribut" /> 
       </td> 
       <td> 
        <input type="value.valor" ng-model="value.midaAtribut" value="value.midaAtribut" /> 
       </td> 
       <td> 
        <input type="checkbox" ng-model="value.atributObligatori" value="value.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" /> 
       </td> 
       <td> 
        <input type="value.valor" ng-model="value.observacions" value="value.observacions" /> 
       </td> 
      </tr> 
      <tr ng-repeat="a in nousAtributs"> 
       <td></td> 
       <td> 
        <input type="number" ng-model="a.ordre" value="a.ordre" /> 
       </td> 
       <td> 
        <input type="a.valor" ng-model="a.nomAtribut" value="a.nomAtribut" /> 
       </td> 
       <td> 
        <input type="a.valor" ng-model="a.midaAtribut" value="a.midaAtribut" /> 
       </td> 
       <td> 
        <input type="checkbox" ng-model="a.atributObligatori" value="a.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" /> 
       </td> 
       <td> 
        <input type="a.valor" ng-model="a.observacions" value="a.observacions" /> 
       </td> 
      </tr> 
     </table> 
     <button class="btn btn-default" > 
      <span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span> 
      Guardar 
     </button> 
    </div> 
</div> 

私はspanタグ

代わりにボタンのタグにNGクリックを置きます

よろしくお願いいたします。

+2

コンソールでエラーを得ていますか? 'newRow'で設定しているプロパティは、テーブルに出力しようとしているプロパティと一致していないようです。たとえば、最初のカラムには 'ng-model =" a.ordre "'がありますが、作成している 'newRow'オブジェクトには" ordre "プロパティはありません。 – Lex

+0

スパンタグの代わりにボタンタグにng-clickを入れました。とにかくありがとう – proktovief

答えて

1

あなたのクリックは、スパンではなくボタンタグ内にある必要があります

0

あなたがしたいと思うのはng-repeat-startng-repeat-endです。だからあなたは持っているだろう。

<tr ng-repeat-start="(key, value) in atrb"> 
    <td></td> 
    <td> 
     <input type="number" ng-model="value.ordre" value="value.ordre" /> 
    </td> 
    <td> 
     <input type="value.valor" ng-model="value.nomAtribut" value="value.nomAtribut" /> 
    </td> 
    <td> 
     <input type="value.valor" ng-model="value.midaAtribut" value="value.midaAtribut" /> 
    </td> 
    <td> 
     <input type="checkbox" ng-model="value.atributObligatori" value="value.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" /> 
    </td> 
    <td> 
     <input type="value.valor" ng-model="value.observacions" value="value.observacions" /> 
    </td> 
</tr> 
<tr style="display:none;" ng-repeat-end></tr> 
<tr ng-repeat-start="a in nousAtributs"> 
    <td></td> 
    <td> 
     <input type="number" ng-model="a.ordre" value="a.ordre" /> 
    </td> 
    <td> 
     <input type="a.valor" ng-model="a.nomAtribut" value="a.nomAtribut" /> 
    </td> 
    <td> 
     <input type="a.valor" ng-model="a.midaAtribut" value="a.midaAtribut" /> 
    </td> 
    <td> 
     <input type="checkbox" ng-model="a.atributObligatori" value="a.atributObligatori" ng-true-value="'Si'" ng-false-value="'No'" /> 
    </td> 
    <td> 
     <input type="a.valor" ng-model="a.observacions" value="a.observacions" /> 
    </td> 
</tr> 
<tr style="display:none;" ng-repeat-end></tr> 

ドキュメントはhere

+0

OPの 'ng-repeat'の使用は正しいので、' ng-repeat-start'と 'ng-repeat-end'は必要ありません。 – Lex

関連する問題