I have a page which uses two controllers, SearchController
and ProductController
, that are both part of one app. They both sit on the main page, and are called using ng-controller
. I have a route set up for when i click 'search' and this loads an ng-view
which uses the ProductController
. This loads up fine, but the problem I'm having is that the div
with ProductController
already on the page doesn't get affected when search is clicked.
Here is the plunker: http://plnkr.co/edit/LnsL0M3KczBmeKsS9uDb?p=preview
HTML - I have a dummy select box. The important part here is the second div with ProductController. This is meant to show up if $scope.products.length > 0. When search is clicked, $scope.products inside of ProductController is filled, but this box isn't affected.
<div ng-controller="SearchController" class="ng-cloak">
<div class="search-form">
<select ng-model="search.category">
...
</select>
<button class="btn btn-primary" ng-disabled="search.category == 0" ng-click="doSearch()">Search</button>
</div>
</div>
<div ng-controller="ProductController" class="ng-cloak">
<div class="criteria" ng-show="products.length > 0">
<div>Category: {{search.category}}</div>
</div>
</div>
<div ng-view></div>
Inside my ProductController i load a products.json file that just brings back everything. So I would think that now that $scope.products.length > 0, the above div should show. But it doesn't. What am I doing wrong?
$http.get('products.json')
.success(function(data) {
$scope.products = data[0].products;
sessionService.loadProducts(data[0].products);
console.log('JSON LOADED-------------');
console.log('products.length: ', $scope.products.length);
});
I'm using console.logs so you can follow what's happening a little better.
Also, once you click search. If you click search again, it doesn't run anything. I'm assuming this is because it's already ran the location.path function. But why does this happen and how can I make it search again?