Ad

Getting The Correct Record In Angular With A Json Feed And Passed Data

- 1 answer

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}}&#8457;</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

Ad

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);
Ad
source: stackoverflow.com
Ad