getting the correct record in Angular with a json feed and passed data
so I have an angular service that picks up a json feed of data, lets say a list of football players - this is working fine. I have a home controller that passes this data through to the home view with code like this...
Home Controller
.controller("mainController", function ($scope, playerService)
{
var promise = playerService.getPlayers();
promise.then(function (data)
{
$scope.players = data;
});
})
Home View
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Club </th>
<th>More?</th>
</tr>
</thead>
<tbody>
<tr data-ng-repeat="player in players">
<td>{{player.name}}</td>
<td>{{player.club}}℉</td>
<td><a target="_blank" rel="nofollow noreferrer" target="_blank" rel="nofollow noreferrer" href="#playercode/{{player.code}}">more</a></td>
</tr>
</tbody>
</table>
As you can see from this the id is then pushed to the next page in the URL
here would be the start of the second controller
var promise = playerService.getPlayers();
promise.then(function (data)
{
var code = $routeParams.code;
$scope.players = data;
$scope.whichResult = $routeParams.code;
console.log($scope.players);
console.log($scope.whichResult);
});
What I need to do now is go through the result from the last data set and just pust to the view the data i need but I am not sure how to do this.
I have the date set in $scope.players and the player code in $scope.whichResult but how do i filter just to get the player I want?
I have 2 main routes setup using ngRoute
var playerApp = angular.module('playerApp', ['ngRoute']);
playerApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'app/views/home/homeview.html',
controller : 'mainController'
})
// route for the about page
.when('/playercode/:code', {
templateUrl : 'app/views/player/playerview.html',
controller : 'playerController'
})
});
Any help much appreciated
Answer
URL: http://server.com/index.html#/Chapter/1/Section/2?search=moby
// Then
$routeParams ==> {chapterId:'1', sectionId:'2', search:'moby'}
Reference: Angular Docs
For creating a URL, you can use $location
e.g.:
$location.path('/chapter/' + Id + '/section/' + secId);
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM