0

I hope my title wasn't too confusing. Basically what I have is a list of portfolio items whose data is gathered from a JSON file. Each portfolio item has a url that will navigate to the single portfolio item.

Here is a sample of the JSON:

[
{
     "url": "nutcracker",
    "name": "Nutcracker Main Street Ballet",
     "snippet": "Trifold Playbill",
     "imgurl": "img/nutcracker.jpg",
     "keyword": "trifold brochure folder over foldover folded"},

MORE
]

And here is the controller that is gathering the data:

myApp.controller('ListCtrl', function ($scope, $http) {
  $http.get('items/items.json').success(function(data){
    $scope.pages = data; 
  });

});

And here, the routing:

config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/list', {templateUrl: 'partials/list.html', controller: 'ListCtrl'});
  $routeProvider.when('/:pageUrl', {templateUrl: 'partials/single-item.html', controller: 'ItemCtrl'});
  $routeProvider.otherwise({redirectTo: '/list'});
}]);

Now each portfolio item uses the 'url' datum to dictate the url. So this item would provide a url of: www.mysite.com/#/nutcracker.

What I want to do is search the URL for 'nutcracker' and then find that corresponding item in the JSON array and use it's data to populate the single portfolio page.

I hope this isn't too confusing. Please ask questions if you think you can help but may not be sure as to what I'm looking for!

Thank you very much!

2
  • there are a few different techniques that could be used to approach this need. Have you tried anything yet and run into a problem, or are you just asking for the best approach? Commented Feb 20, 2014 at 5:09
  • I'm asking because I'm a beginner at Angular and honestly don't know where to start. Commented Feb 20, 2014 at 5:15

1 Answer 1

1

Use $location.path() to access the path, strip off the leading "/", then use the path to search the array (example uses $.grep() to do this because question was tagged jQuery)...

myApp.controller('ListCtrl', function ($scope, $http, $location) {
  $http.get('items/items.json').success(function(data){
    $scope.pages = data; 

    var path = $location.path();  // "/nutcracker"
    if (path.length) {
      path = path.substring(1); // "nutcracker"
    }
    // find matching page in pages
    $scope.page = $.grep(pages, function (page) { return page.url === path; })[0];
  });

});
Sign up to request clarification or add additional context in comments.

5 Comments

i'm thinking if you take this approach, $location will cause the app to re-route, which will cause a redirect loop. +1 for use of $grep
Does calling $location.path() trigger a route change? I thought it just returns the current route. At least that's what the docs seem to suggest? docs.angularjs.org/api/ng/service/$location
Ok Anthony, let me try this method in the morning... It is getting late here. I will update you if this helps me. From looking at it, it looks like it might be in the right direction.
yeah, you're right. My bad. you need to return with it or pass it params in order to trigger a route change.
Sorry for taking so long to respond. This didn't seem to work for what I was doing. When the url changes example.com/#/nutcracker it doesn't extract just the object with the corresponding url and so it doesn't extract any data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.