ng-repeatを使用してページに表示されている項目のリストがあります。以下はhtmlです。ng-repeatはコントローラー内の配列の変更を描画しません
<div class="container-fluid">
<div class="container" >
<div class="page-header">
<h3>Found {{searchResults.length}} results that match your search <br /><small>Click on any result to view details</small></h3>
</div>
----
<div class="list-group" ng-repeat="item in searchResults track by $index">
<a ng-href="#" class="list-group-item">
<h4 class="list-group-item-heading">{{item.test_name}}</h4>
<p class="list-group-item-text" ng-bind-html="item.synonyms"></p>
</a>
</div>
</div>
この配列は、ときに、アプリケーションのロード作成されます。以下は、配列が変更されたコントローラ内の関数です。
ユーザーが検索ボックスに何かを入力して検索すると、コードに示されているように場所を変更し、ngrepeatディレクティブを使用して結果のリストを表示します。
$scope.onClickSearch = function() {
console.log("Search Click in Controller - " + $scope.searchString);
$scope.searchResults = new Array();
/*
remove special characters and html tags from the synonyms and check if the name
or synonym contains the search string.
*/
for (i = 0; i < $scope.items.length; i++) {
var synonyms = $scope.items[i].synonyms
var testName = $scope.items[i].test_name.toLowerCase();
if ((testName.indexOf($scope.searchString) > -1) || (synonyms.indexOf($scope.searchString) > -1)) {
var searchItem = $scope.items[i];
$scope.searchResults.push(searchItem);
}
};
$location.url("/search");
});
console.log($scope.searchResults); //Array is shown to be updated here.
この関数が配列を更新すると、ページに表示されるhtmlは変更されません。 コンソールのログに正しいことが表示されます。試しました$scope.$digest()
と$scope.$apply()
結果に変更はありません。
ここには再作成された問題があるplunkerがあります。これにより、問題の詳細な説明が得られるはずです。検索ボタンを押すと、検索結果が表示されません。
私はこの1つに似た2番目の質問を開いていますが、回答はありませんでした。これは私の2番目の試みで、少し異なる説明があります。
は、あなたはそれがその後、変更された場合を参照してください方法の外で$ scope.searchResultsを宣言したことがありますか? – rrd
これは私にいくつか考えることができます。メソッドの外側に空の配列があります – pparas
$ scope.itemsの結果をそのコントローラに記録できますか?内容はありますか? – rrd