I'm trying to learn how to use AngularJS UI Router in a small search app but can't seem to get the query parameters right.
In Chrome's address bar, it never changes from this:
http://localhost:8000/app/search?q=searchTerms
Instead of http://localhost:8000/app/search?q=typeduserinput
I have this for .config and .run:
searchApp.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider) {
$locationProvider.html5Mode(true);/*for browser history, back and forward buttons*/
$stateProvider
.state('home', {
url: '/home',
//params: {'searchTerms': 'null'},
templateUrl: 'home/home.html', controller: 'homeCtrl'})
.state('search', {
url: '/search?searchTerms',
//url: '/search?q',
params: {'q': 'searchTerms'},
views: {
'' : { templateUrl: 'search/search.html', controller: 'searchCtrl' }
//add more views here when necessary
}
});
$urlRouterProvider.otherwise('/home');
}]);
searchApp.run(function ($rootScope, $location) {
$rootScope = $location;
});
So my home page is real simple for now. Just a searchbox that provides autocomplete functionality.
I have this in my homeCtrl:
$scope.searchTerms = null;
$scope.searchTerms = $stateParams.searchTerms;
And my ng-submit button calls a search(), which has this code:
$location.path('/search').search({'q': 'searchTerms'});//chaining location, state and url parameters
Where am I going wrong trying to get the query parameters and ui router?
UPDATE
I've been doing lots of reading to better understand how UI Router fits into what I'm trying to do. I think I'm starting to "get it" now. My first mistake was that I have 2 controllers and both were loading ui.router as a dependency which seems to cause Error: Could not resolve '/search' from state 'home'
because the the 'search' state has not been loaded yet - and only 1 can declare ui.router as dependency - however, but both js files must be loaded.
So.... I think it makes sense to have homeCtrl (autocomplete) load ui.router and then have searchCtrl (searching) load homeCtrl... does that make sense, am I understanding this correctly? Would it make better practice to make a "routes" module that loads ui.router as a dependency as well and have homeCtrl load the routes module as a dependency?