2017-02-14 5 views
0

私の機能は非常に簡単で、非常に一般的なことを想像しています。私は '年齢グループ'のコレクションをリストアップし、 '年齢グループ'を追加する機能を提供するページを持っています。新しい「年齢層」が追加されたときに、そのページにすぐに変更が反映されるようにしたいと考えています。

初期のバージョンでは、サービスで$ rootScopeを使用して、$ http要求が完了したときにすべての「年齢グループ」を$ rootScopeに入れました。この最新のリファクタリングでは、私のアプリケーションで$ rootScopeの使用をすべて削除していて、問題になってきています。次のように

HTMLビューのコードは次のとおりです。[注:余分考慮されるすべてのコードは、すべてのコードスニペットから削除されました]

<div id="tabContent" class="tab-pane fade in active" ng-show="subIsSelected(1)"> 
    <div class="row"> 
     <div class="col-xs-12"> 
      <form class="form-inline"> 
       <button type="button" class="btn btn-success" ng-click="openAddAgeGroup()">Add Age Group</button>     
      </form> 
     </div> 
    </div> 
    <hr /> 
    <div class="row row-content">      
     <div class="col-xs-12"> 
      <h4 ng-show="!areAgeGroups()">No current age groups.</h4> 
      <ul class="media-list" ng-show="areAgeGroups()"> 
       <li class="media" ng-repeat="ageGroup in ageGroups" style="padding:10px; background: lightGray;"> 
        <div class="media-left media-top" > 
         <img class="media-object img-thumbnail" style="width: 75px;" ng-src="./images/trfc.png" alt="club logo"> 
        </div> 
        <div class="media-body"> 
         <h3 class="media-heading" style="padding-top: 20px;">{{ageGroup.name}}</h3> 
         <button type="button" class="btn btn-xs btn-primary" style="width: 50px;" ng-click="openEditAgeGroup(ageGroup)">Edit</button> 
         <button type="button" class="btn btn-xs btn-danger" style="width: 50px;" ng-click="deleteAgeGroup(ageGroup)">Delete</button> 
        </div> 
       </li> 
      </ul> 
     </div> 
    </div> 
</div> 

最初にロードされ、ページが正しく、すべての年齢グループを表示するとき'は$ scope.ageGroups配列にあります。

$scope.openAddAgeGroup = function() { 
    console.log("\n\nOpening dialog to add age group"); 
    ngDialog.open({ template: 'views/addAgeGroup.html', scope: $scope, className: 'ngdialog-theme-default', controller:"HomeController" }); 
}; 

をし、そのダイアログがよう移入さ:「年齢層」、次のようにNG-ダイアログが作成されますを追加するボタンをクリックすると

<div class="ngdialog-message"> 
    <div class=""> 
     <h3>Add a New Age Group</h3> 
    </div> 
    <div>&nbsp;</div> 
    <div> 
     <form ng-submit="addAgeGroup()"> 
      <div class="form-group"> 
       <label class="sr-only" for="name">Age Group Display Name</label> 
       <input type="text" class="form-control" id="name" placeholder="age group name" ng-model="ageGroupForm.name"> 
      </div> 
      <div class="form-group"> 
       <label class="sr-only" for="birthyear">Birth Year</label> 
       <input type="text" class="form-control" id="birthyear" placeholder="birth year" ng-model="ageGroupForm.birthyear"> 
      </div> 
      <div class="form-group"> 
       <label class="sr-only" for="socceryear">Soccer Year</label> 
       <div class="input-group"> 
        <div class="input-group-addon">U</div> 
        <input type="text" class="form-control" id="socceryear" placeholder="soccer year" ng-model="ageGroupForm.socceryear"> 
       </div>        
      </div> 
      <button type="submit" class="btn btn-info">Add</button> 
      <button type="button" class="btn btn-default" ng-click=closeThisDialog("Cancel")>Cancel</button> 
     </form> 
    </div> 
</div> 

するとフォームコントローラから「年齢グループ」がデータベースに追加されます。

'use strict'; 

angular.module('ma-app') 
    .controller('HomeController', ['$scope', 'ngDialog', '$state', 'authService', 'coreDataService', 'userService', '$rootScope', 'clubService', 'schedulingService', function($scope, ngDialog, $state, authService, coreDataService, userService, $rootScope, clubService, schedulingService) { 

... 

    $scope.addAgeGroup = function() { 
     coreDataService.addAgeGroup($scope.ageGroupForm) 
      .then(function(response) { 
       coreDataService.refreshAgeGroups() 
        .then(function(response) { 
         coreDataService.setAgeGroups(response.data); 
         $scope.ageGroups = coreDataService.getAgeGroups(); 
         console.log("\n\n\nretrieved age groups and put them in scope"); 
         console.log($scope.ageGroups); 
         ngDialog.close(); 
        }); 
      }, function(errResponse) { 
       console.log("Failed on attempt to add age group:"); 
       console.log(errResponse); 
      }); 
    }; 

coreDataServiceはdefiです次のように定義さ:「年齢層」が追加されたときに

'use strict'; 

angular.module('ma-app') 

    .service('coreDataService', ['$http', 'baseURL', 'googleGeolocateBaseURL', 'googleGeocodeKey', 'googleMapsBaseURL', function($http, baseURL, googleGeolocateBaseURL, googleGeocodeKey, googleMapsBaseURL) { 
     var ageGroups = {}; 
     var ageGroupsLoaded = false;  


     this.getAgeGroups = function() { 
      return ageGroups; 
     }; 

     this.setAgeGroups = function(newAgeGroups) { 
      ageGroups = newAgeGroups; 
      ageGroupsLoaded = true; 
     }; 

     this.addAgeGroup = function(formData) { 
      //post age group:    
      var postString = '{ "birth_year": "' + formData.birthyear + '", "soccer_year": "U' + formData.socceryear + '", "name": "' + formData.name + '" }'; 
      console.log("Posting age group with string: " + postString); 
      return $http({ 
       url: baseURL + 'age_groups/', 
       method: 'POST', 
       headers: { 
        'content-type': 'application/json' 
       }, 
       data: postString 
      }); 
     };  
    }]); 

をので、コンソールロギングは、新しい「年齢層」がコレクションに今$ scope.ageGroups配列に格納されていることを示しますが、HTMLのNG -repeatは新しい「年齢層」を反映しません。インターフェイスの別のタブに移動し、「年齢グループ」を含むタブに戻ると、新しく追加された「年齢グループ」が表示されます。

+0

「ng-repeat」部分のコントローラは何ですか?あなたは追加ダイアログとng-repeatのために同じコントローラを使用していますか? – Yaser

+0

はい、どちらもHomeControllerを使用します –

+0

coreDataService.refreshAgeGroups()は何をしますか? – Chait

答えて

0

更新日: 値を更新した後、更新されたことを角度で伝える必要があります。コードを$ timeoutにラップした場合。次のダイジェストサイクルで、ビューが更新されます。詳細については、hereを参照してください。

... 
$timeout(function() { 
    $scope.ageGroups = coreDataService.getAgeGroups(); 
    // anything you want can go here and will safely be run on the next digest. 
}) 
... 

$ timeoutは基本的に次の$ digestサイクルで実行され、ビュー内の値を更新します。

+0

$ scope。 $ apply()は進行中の例外をスローします。つまり、$ applyを別のものの中にラップします。 –

+0

私の更新された答えを見てください。それはまあまあですが、あなたのコードを$タイムアウトでラップすることができます。ダイジェストサイクルに変更内容が含まれていることを確認します。 '$ timeout'をコントローラ内の依存関係として追加してください。 – matt

+0

ない良い、同じ結果 'coreDataService.refreshAgeGroups() .then(関数(応答){ coreDataService.setAgeGroups(response.data); $タイムアウト(関数(){ $ scope.ageGroups = coreDataService.getAgeGroups() ; }) '同じ結果を返します –

0
私は directiveであなたの ng-repeat部分を包むことをお勧め

は、addメソッドにイベントを発生し、指令から、それに耳を傾ける:

$scope.$on('itemadded', function() { 
    $scope.ageGroups = coreDataService.getAgeGroups(); 
}); 
0

I:あなたのディレクティブリンク機能インサイド

this.addAgeGroup = function(formData) { 
    // do stuff 
    $scope.$broadcast('itemadded'); 
} 

それがバインドされている配列が更新された場合、ng-repeatをリフレッシュするControllerAs構文を使用することをお勧めします。

ここでの回答を参照してください - ng-repeat not updating on update of array

関連する問題