私はAngular 1.5.9
とangular-route 1.5.9
を使用しています。私は実用的な解決策を持っていますが、DOMを更新するには$scope.$apply()
が必要であると私には理解できません。
ng-repeat
として使用されている配列を更新するために、工場とコントローラを使用しています。私は、ダイジェストサイクルが引き起こされていないことを理解しています。そのため、$scope.apply()
を使用する必要がありますが、理由はわかりません。ここで失敗しているコードは次のとおりです。
.when('/mygames' ,{
templateUrl: '/views/templates/mygames.html',
controller: 'MyGamesController',
controllerAs: 'mygames'
})
そしてHTMLのngのリピート部分のHTMLは次のようになります:
myApp.controller('MyGamesController', ['$http', '$firebaseAuth', 'DataFactory', '$scope', function ($http, $firebaseAuth, DataFactory, $scope) {
console.log('mygamescontroller running');
var self = this;
self.newGame = {}
self.games = [];
getGames();
function getGames() {
DataFactory.getGames().then(function (response) {
console.log('returned to controller from factory', response); // logs the correct response including lastest data, but DOM doesn't update
self.games = response; // self.games is correctly set, but not updated on the DOM
$scope.$apply(); // Updates the DOM
});
} //end getgames function
self.addGame = function() {
DataFactory.addGame(self.newGame).then(getGames);
}
}]); //end controller
角度ルータはこれのように私のcontrollerAs
を処理しています
<tbody>
<tr ng-repeat="game in mygames.games">
<td>{{game.game}}</td>
<td>{{game.number_players}}</td>
<td>{{game.time_to_play}}</td>
<td>{{game.expansion}}</td>
</tr>
</tbody>
私はself.games
アレイをクリアし、それぞれをプッシュして問題を解決しなかったループを作成しようとしました。私はまだDOMを更新するために$scope.$apply()
が必要でした。
それは場合に役立ちます私はここに利用できる全体のレポ(特定のコミットリンク)を持っている:https://github.com/LukeSchlangen/solo_project/tree/df72ccc298524c5f5a7e63f4a0f9c303b395bafa
変更 'VARの自己=この;' VARの自己= $スコープ 'には、'あなたの配列、 '自己を更新する必要があります。ゲーム ' – tsuz