基本的に2つのコントローラーを作成しました。最初のコントローラーがフォーム用で、もう1つが最初のコントローラーからの結果を使用する2つのコントローラー間でデータを共有するにはどうすればいいですか?
最初のコントローラは、検索バーからの入力を受け取り、$ http要求を行うだけです。コントローラからの検索機能は次のとおりです。 -
$scope.search = function() {
$scope.searchString &&
searchService.search($scope.searchString).then(function(results) {
//what should I do here to share these results with the second controller?
});
$scope.searchString = null;
}
2番目のコントローラは、これらの結果を基本的に表示する必要があります。しかし、少なくとも私はそこにconsole.logに結果を得ることができますか?
基本的に検索があり、結果があります。
私は検索フォームを送信すると、結果はあなたがまた
$scope.search = function() {
$scope.searchString &&
searchService.search($scope.searchString).then(function(results) {
//what should I do here to share these results with the second controller?
// if not working $emit then use $broadcast instead of $emit
$rootScope.$emit('someEvent', results);
});
$scope.searchString = null;
}
function displayController($scope)
{
$rootScope.$on('someEvent', function(event, args) {
console.log(args);
});
}
ありがとう。whats the diff $ scopeと$ rootScopeの間の違いは? – user1354934
心配しないでください。今働いて!どうもありがとう! – user1354934