Overview
I am creating an application (runs on MAMP) that contains contact information that will expand to store more data, such as the name of the project and the deadline, as soon as this part is operational.
Questions
When a user visits /projects.php#/project/ , I would like them to see a list of all the project names with a link to their details page.
- How to write the following to access all my data?
Do I need .json at the end?
What does @id do?
return $resource('data/project.json/:id', {id: '@id'});
When a user visits /projects.php#/project/a-gran-goodn , I would like them to see details about this project (now just a name and address).
- How can I write the following to return my data by identifier?
$scope.project = $routeParams.id ? Project.get({id: $routeParams.id}): new Project();
plunkr file
http://plnkr.co/edit/7YPBog
project.json
This file is located at http://localhost:8888/angularjs/ProjectsManager/data/project.json
[ { "address" : [ " 3156 Dusty Highway", " Teaneck New Jersey 07009-6370 US" ], "id" : "a-gran-goodn", "name" : "Grania Goodner", "phone" : " (862) 531-9163" }, { "address" : [ " 62 Red Fawn Moor", " Rodney Village West Virginia 25911-8091 US" ], "id" : "b-aime-defranc", "name" : "Aimery Defranco", "phone" : " (681) 324-9946" } ]
app.js
var projectsApp = angular.module('projects', ['ngResource']); projectsApp.config(function($routeProvider) { $routeProvider .when('/', { controller: 'ProjectListCtrl', templateUrl: 'partials/projectlist.html'}) .when('project/:id', { controller: 'ProjectDetailCtrl', templateUrl: 'partials/projectdetail.html' }) .otherwise('/'); }); projectsApp.factory('Project', function($resource) { return $resource('data/project.json/:id', {id: '@id'}); }); projectsApp.controller('ProjectListCtrl', function(Project, $scope) { $scope.projects = Project.query(); console.log($scope.projects); }); projectsApp.controller('ProjectDetailCtrl', function(Project, $routeParams, $scope) { $scope.project = $routeParams.id ? Project.get({id: $routeParams.id}) : new Project(); });
overtones /projectlist.html
<a href="#/project/" class="btn">Add new item</a> <ul class="unstyled"> <li ng-repeat="project in projects"> <div class="well"> <h2><small>{{project.id}}</small> {{project.name}}</h2> <a href="#/project/{{project.id}}" class="btn btn-link">View Info for {{project.name}}</a> </div> </li> </ul>
overtones /projectdetails.html
<h3>Information</h3> <p>Name: {{project.name}}</p> <p>Phone Number: {{project.phone}}</p> <p ng-repeat="line in project.address">{{line}}</p>
index.php
<?php header('Access-Control-Allow-Origin: *'); ?> <!doctype html> <html ng-app="projects"> <head> <meta charset="utf-8"> <title ng-bind="title" ng-cloak>Restaurant —</title> <link href="https://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.no-icons.min.css" rel="stylesheet"> </head> <body ng-controller="ProjectListCtrl"> <a class="brand" href="#">Projects Manager</a> <div id="app-container" class="container-fluid"> <div class="row-fluid"> <div class="span12" id="main" ng-view> </div> </div> <footer>Copyright Projects © 2013</footer> </div> <script src="http://code.jquery.com/jquery-1.10.0.min.js"></script> <script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.js></script> <!--Controllers--> <script src="app.js"></script> </body> </html>