I have two controllers - searchBoxController and productList. What I'm trying to do is update the scope scope.products variable from multiple controllers. I know that defining it as a root variable is a very poor design, but using this in a shared service does not solve the problem. The update is not reflected in the HTML templates!
function SearchTermService(){
this.productSearch = function(data, $http){
var url = "";
$http.get(url).then(function(resp){
return resp.data;
},
function(err){
console.log(err);
});
};
};
var app = angular.module('app', []);
app.service("myService", MyService);
app.service("searchTermService", SearchTermService);
app.run(function($rootScope) {
$rootScope.products = new Date();
});
app.controller('productList', function ($scope, $rootScope, $http, myService) {
$rootScope.products = prod_res;
});
app.controller('searchBoxController', function($scope, $http, searchTermService, $rootScope){
$scope.getSearchResults = function(){
$rootScope.products = searchTermService.productSearch($scope.term, $http)
};
});
PS: I'm not sure that I need to return the promise when assigning $ rootScope.products to 'searchBoxController', since console.log says it is undefined. Currently, I am not returning a promise from the service.
source
share