2017-11-15 12 views
2

が、私はこれらのはリピートngの

{ 
    "data":{ 
    "xyz":[ 
     { 
     "number":"1", 
     "short_text":"Vertrag unterzeichnen", 
     "long_text":"Nach Vertrabsunterzeichnung Namen eintragen", 
     "is_photo":false 
     }, 
     { 
     "number":"2", 
     "short_text":"HR unterrichten", 
     "long_text":"HR hat eigene Workflows", 
     "is_photo":true 
     } 
    ] 
    } 
} 

のようなバックエンドからJSONデータを受信して​​いますし、HTMLに私は、フォームを移入していますAngularJSで条件に基づいて必要な入力タイプのファイルを作成します

<tr data-ng-repeat="choice in choices track by $index"> 
    <td>{{choice.number}}</td> 
    <td><p>{{choice.short_text}}</p></td> 
    <td><input type="textbox" size="50" class="des-textinput" ng-model="choice.desc" required></td> 
    <td><input type="checkbox" ng-model="choice.include"></td> 
    <td><input type="file" id="abc{{$index}}" class="photo-upload" 
       file-model="pic{{$index}}" accept="image/*"> 
    </td> 
</tr> 

今私は受け取っているJSONのis_photoの値がtrueであれば、入力タイプファイルを必要としたいと思っています。各行について、値がis_photoの場合、falseである必要はありません。条件は、第1の入力タイプのファイルであろう所与JSONから

が最初の行のように必要ではないであろうis_photofalseであるが、is_photoの値がtrueであるように、第2の一つが必要となります。

どうすればよいですか?あなたがドキュメントについては "NG-必要"

を使用することができます

+1

フォームのコードも記入してください。 – 31piy

+0

あなたの 'file-model'ディレクティブを[ng-modelコントローラ](https://docs.angularjs.org/api/ng/type/ngModel.NgModelController)と[ng-formコントローラ] (https://docs.angularjs.org/api/ng/type/form.FormController)、その指令のコードを投稿する必要があります。 – georgeawg

答えて

2

あなたはこの読むことができる:あなたはこの使用に

を使用して、この

<input name="myInput" ng-model="myInput" ng-required="myVar == 2"> 

//If photo is true required will be true else false 
<input name="myInput" ng-model="myInput" ng-required="_photo"> 

<input name="myInput" ng-model="myInput" ng-required="choice.is_photo"> 

//Or use some function which returns boolean 
<input name="myInput" ng-model="myInput" ng-required="isRequired(choice)"> 

//This is how you would use it with form and stop form from submittion  
<form ng-app="myApp" ng-controller="validateCtrl" 
     ng-init="isRequired=true" 
     name="myForm" novalidate ng-submit="myForm.$valid && submit()"> 
     Username: <input type="text" name="user" ng-model="user" 
         ng-required="isRequired"> 
     Email : <input type="email" name="email" ng-model="email" required> 
     <input type="submit" ng-click="isRequired=!isRequired;" /> 
</form> 

    <script> 
     var app = angular.module('myApp', []); 
     app.controller('validateCtrl', function($scope) { 
      $scope.user = 'John Doe'; 
      $scope.email = '[email protected]'; 
      $scope.submit =() => {console.log("s");} 
     }); 
    </script> 
+0

あなたのフォームが無効であることをチェックしていないので、フォームは – Samim

+0

を送信できません...この

Hey24sheep

+0

既にそこにはありますが、動作しません。 – Samim

0

ng-requiredプロパティを使用できます。 ref ng-required

<input type="file" id="abc{{$index}}" class="photo-upload" 
     file-model="pic{{$index}}" accept="image/*" 
     ng-required="choice.is_photo == true" />