angularjs - Binding to data when changing the URL -
angularjs - Binding to data when changing the URL -
i building application uses ui-router. on /teams route have form user can add together team name pushes name mongodb , binds view , displays team name several options, 1 of beingness button links page more specific info can added team. have routing working on , url appears /#/teams/oklahoma
or /#/teams/washington
example. here routing looks like:
app.config( ['$stateprovider','$urlrouterprovider', function($stateprovider, $urlrouterprovider){ $urlrouterprovider.otherwise('/'); .state('teams', { url: '/teams', templateurl: './templates/main/league.html', controller: 'leaguectrl' }) .state('teams/:title', { url: '/teams/:title', templateurl: './templates/main/team.html', controller: 'teamctrl' })
here link /teams/:title
route:
<a href="#subjects/{{ team.title | lowercase }}"> <button ng-click="viewteam(team)" class="team-button">view</button> </a>
currently not have in viewteam() function. question how bind {{ team.title }} , other related info in new view new url? know mill must somehow involved , have tried implementing solution described @ http://onehungrymind.com/angularjs-communicating-between-controllers/ without success. additional guidance much appreciated.
the url should contain team id. i'm going assume 'teams' array loaded using $http backend api.
app.config( ['$stateprovider','$urlrouterprovider', function($stateprovider, $urlrouterprovider){ $urlrouterprovider.otherwise('/'); .state('teams', { url: '/teams', // `teams` state wait load until $http.get() phone call completed. // resolve declaration provide resulting teams array injectable 'teams' object. resolve: { teams: function($http) { homecoming $http.get("/teams.json"); }, templateurl: './templates/main/league.html', // simplified controller injects 'teams' resolve , sticks list of teams on scope ng-repeat etc. controller: function($scope, teams) { $scope.teams = teams; } }) // nested state, called `teams.team`. requires `teamid` parameter. // parameter can provided url: http://localhost/app#/teams/9237594827 // parameter can provided $state.go: $state.go('teams.team', { teamid: 9237594827 }); // parameter can provided uisref: <a ui-sref="teams.team({ teamid: repeatedteam.id })">{{repeatedteam.title}}</a> .state('teams.team', { // `teams.team` state declares `teamid` state parameter in url url: '/teams/:teamid', // resolve function uses $stateparams locate right team list. // `team` made available injection resolve: { team: function($stateparams, teams) { // find right team `teams` array, id. homecoming teams.filter(function(team) { homecoming team.id === $stateparams.teamid; })[0]; } }, templateurl: './templates/main/team.html', // inject `team` resolve , set on $scope controller: function($scope, team) { $scope.team = team; } })
league.html:
<ul> <li ng-repeat="team in teams"> <a ui-sref="teams.team({ teamid: team.id })">{{team.title}}</a> </li> </ul>
angularjs mongodb angular-ui-router
Comments
Post a Comment