0

私はng-repeatで動的フォームに取り組んでいます。私はoi-selectを使って私の位置を読み込んでいます。このフォームをモーダルポップアップの中に読み込んでいます。私の場所の値を取得することについては、私はモーダルオープンボタンをクリックして1つのAPI関数を呼び出しています。このAPI呼び出しが成功すると、私はng-initメソッドを呼び出していました。私の要件として私はモーダルオープンボタンのクリックでこのAPI呼び出しを行う必要があります。最初にそのボタンをクリックしている場合、デフォルトでは、最初の位置値は選択ボックス内に読み込まれません。私のng-init関数が私のapi呼び出しの前に呼び出されたからです。初めてのこと以外はその罰金。初めてlocation [0]値がデフォルトでロードされていません。私の質問は、これを遅らせる方法ですng-init機能?この問題を解決する他の方法はありますか?ここでは、私のplunkerリンクを添付しました。この問題から私を助けてください。私のAPI呼び出しが成功するまで、ng-init関数を遅らせる方法

<div class="col-md-3" ng-if="locationsLoaded"> 
    <label>Location</label> 

    <oi-select ng-model="user.location" multiple oi-options="v for v in locations" ng-init='initLocation(user, locations)' name="select2" required> 

    </oi-select> 
</div> 

あなたはc:あなたはOI-選択親に条件を置くことができるhttps://plnkr.co/edit/xDVetaZIzpFdKLS9ihU6?p=preview

htmlコード

<body class="container row"> 
<div ng-app="myApp"> 
    <div ng-controller="myCtrl"> 
     <a class="btn btn-primary" data-toggle="modal" href='#modal-id' ng-click="getLocation()">Trigger modal</a> 
     <div class="modal fade" id="modal-id"> 
      <div class="modal-dialog"> 
       <div class="modal-content"> 
        <div class="modal-header"> 
         <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> 
         <h4 class="modal-title">Modal title</h4> 
        </div> 
        <div class="modal-body"> 
         <form role="form" name='userForm' novalidate> 
          <div class="container"> 
           <div class="row" ng-repeat="user in users"> 
            <div class="form-group"> 
             <div class="col-md-3"> 
              <label>ID</label> 
              <input ng-model="user.id" id="user.id" name="user.id" placeholder="Enter bugid" type="text" required readonly disabled> 
             </div> 
             <div class="col-md-3"> 
              <label>Comments</label> 
              <textarea ng-model="user.comment" id="textarea1" rows="1" required></textarea> 
             </div> 

             <div class="col-md-3 "> 
              <label>Location</label> 

              <oi-select ng-model="user.location" multiple oi-options="v for v in locations" ng-init='initLocation(user, locations)' name="select2" required> 

              </oi-select> 
             </div> 
            </div> 

           </div> 
          </div> 
          <div class="buttonContainer text-center btn-container"> 
           <br> 
           <button ng-disabled="userForm.$invalid" type="button" id="adduser" ng-click="adduser()">Add user</button> 
           <button type="button" class="btn button--default btn--small pull-center">Close</button> 
          </div> 
         </form> 
        </div> 

       </div> 
      </div> 
     </div> 
     <script src="https://code.jquery.com/jquery.min.js"></script> 
     <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css" rel="stylesheet" type="text/css" /> 
     <script src="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> 
    </div> 
</div> 

JSコード

var app = angular.module('myApp', ['ngResource', 'oi.select']); 

app.factory('commonService', function($http, $q) { 
return { 
    preFCSManualReleases: function() { 
     var deferred = $q.defer(); 
     var url = "data.json"; 
     $http.get(url, { 
      headers: { 
       'Content-Type': 'application/json', 
       'Accept': 'application/json' 
      }, 
      data: '' 
     }).then(function(data) { 
      deferred.resolve(data.data); 
     }); 

     return deferred.promise; 
    } 
} 
}); 
app.controller('myCtrl', function($scope, commonService) { 
$scope.getLocation = function() { 
    $scope.ids = [1, 2]; 
    commonService.preFCSManualReleases().then(function(data) { 

     $scope.locations = data; 
     console.log('$scope.location[0]==============>', $scope.locations[0]) 
     $scope.initLocation = (user, locations) => { 
      if (!user.location.length) { 
       user.location.push(locations[0]); 
      } 
     } 
    }); 

    $scope.users = $scope.ids.map(function(id) { 
     return { 
      id: id, 
      comment: "", 
      location: [] 
     }; 
    }); 
} 
$scope.adduser = function() { 
    var data = $scope.users.map(function(user) { 

     return { 
      "userid": user.id, 
      "manualcomment": user.comment, 
      "location": user.location 
     } 
    }); 

    console.log("data", data) 

} 

    }); 

答えて

0

その後、あなたのコントローラで$ scope.locationsLoadedを制御します。

$scope.getLocation = function() { 
    $scope.locationsLoaded = false; 
    commonService.preFCSManualReleases().then(function(data) { 

     $scope.locations = data; 
     console.log('$scope.location[0]==============>', $scope.locations[0]) 
     $scope.initLocation = (user, locations) => { 
      if (!user.location.length) { 
       user.location.push(locations[0]); 
      } 
     } 

     $scope.locationsLoaded = true; // we now have the required data 
    }); 
} 

NG-場合locationsLoadedがtrueで、NG-initはトリガされたとき、あなたはデータがあることを確信しているいったん要素をコンパイルします。

関連する問題