2016-04-07 5 views
0

私はルールのリストを設定するテーブルを持っています。チェックボックスがクリックされると、私はこれらの "真の"値をとり、APIエンドポイントに投稿する必要があります。これは設定されていますが、私が戻ってきたのは "associated_rule"が定義されていないということです。postメソッドのanglejsのチェックボックスの結果の表示

コントローラに$scope.associated_rule.selected = true;を設定しようとしましたが、これは変数を定義しておらず、同じエラーをコンソールにスローします。ここで

が私のHTMLフォームです:

<form name="rules_form" method="post" ng-submit="attach()"> 
    <table class="table table-striped table-hover" ng-model="associated_rules"> 
    <thead> 
    <th>Rule Types:</th> 
    <th>Description:</th> 
    <th>Start Time:</th> 
    <th>End Time:</th> 
    <th>Apply Rule to Vehicle:</th> 
    </thead> 
<tr ng-repeat="associated_rule in associated_rules"> 
<td>@{{ associated_rule.resource_ids.accounts }}</td> 
<td>@{{ associated_rule.description }}</td> 
<td>@{{ associated_rule.start_time }}</td> 
<td>@{{ associated_rule.end_time }}</td> 
<td><input type="checkbox" ng-model="associated_rule.selected" aria-label="rule"></td> 
</tr> 
    </table> 
    <button class="btn btn-primary" ng-click="attach()">Attach</button> 
</form> 

私のコントローラのイベント:

$scope.attach = function() { 
     $scope.associated_rule.selected = true; 
     var i; 
     for (i = 0; i < $scope.associated_rule.selected.length; i++) { 
      //need to create a loop where the true value is picked up and then I can send the data using a POST method. But I'm stuck on this. 
     } 
     console.log(the result of the event); 
    }; 

今のところ、私は結果がCONSOLE.LOGしたいので、私は、イベントが作成されていることがわかりますループを実行して結果を表示します。その後、私はOKになるはずです。

ご協力いただければ幸いです。

答えて

0

私は空の配列として$ scope.ruleを定義し、デフォルトで$ scope.rule.selectedを "false"に設定することでこれを解決しました。

素晴らしい!第一歩!しかし、チェックボックスは、チェックボックスをクリックするとすべて選択されます - これは、ng-repeatである可能性があるため、背面に痛みを感じます。

$scope.rule = []; 
    $scope.rule.selected = false; 

このように、選択したチェックボックスのみをすべてではなく一度に確実に設定するにはどうすればよいですか?

0

これも修正されました。上記のように、配列全体を選択するだけでした。私はアレイに掘り下げていないので。これは、定義された配列内のルールをモデル化することによって、テスト時に表示されます:

ng-model="rules.selected[associated_rule.id]" 

Brill。 :)間違いによって

0

あなたはボタンをクリックすると上のチェックボックスの値を変更している。

$scope.associated_rule.selected = true; 

これは、現在の値を与える、

$log.log($scope.associated_rule.selected); 
を選択したり、選択していません
関連する問題