Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> AngularJS - przekazywanie parametru
PawelC
post 9.06.2018, 09:08:00
Post #1





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

Ostrzeżenie: (0%)
-----


Witam,
Uczę się AngularJS i mam mały problem. Oto mój kod:
Plik routingu
  1. var myApp = angular.module('myApp', ["ngRoute"])
  2. myApp.config(['$routeProvider',
  3. function ($routeProvider) {
  4. $routeProvider
  5. .when('/test', {
  6. templateUrl: '/event/example.html',
  7. controller: 'exampleController'
  8. }, null)
  9. .when('/list', {
  10. templateUrl: '/event/list.html',
  11. controller: 'exampleController'
  12. }, null)
  13. .when('/test-list', {
  14. templateUrl: '/test/list.html',
  15. controller: 'testController'
  16. }, null)
  17. .when('/test/:id', {
  18. templateUrl: '/test/details.html',
  19. controller: 'testController'
  20. }, null)
  21. }
  22. ]);

Controller
  1. angular.module('myApp').controller('testController', function ($scope, $http, $routeParams) {
  2. $http.get('https://jsonplaceholder.typicode.com/posts').then(function (response) {
  3. $scope.posts = response.data;
  4. });
  5.  
  6. $scope.id = $routeParams.id;
  7.  
  8.  
  9. });

Plik details.html
  1. <div data-ng-controller="testController">
  2. {{data}}
  3. </div>

plik list.html
  1. <div data-ng-controller="testController">
  2. <ul>
  3. <li ng-repeat="post in posts">
  4. Tytuł: {{post.title}} <a href="#!test/{{post.id}}" >Show</a>
  5. </li>
  6. </ul>
  7. </div>

Pobieram sobie dane z jsonplaceholder i listuję w pliku.html, tworzę url z id danego postu, teraz chciałbym pobrać ten jeden konkretny rekord. Wartość id pobieram sobie z $routeParams. Mój problem polega na tym, że nie wiem jak teraz po wejściu pod url test/1 pobrało mi z jsonplaceholder post o id np 1, url do pobrania szczegółów danego posta to jsonplaceholder.typicode.com/posts/1

Go to the top of the page
+Quote Post
aras785
post 9.06.2018, 22:35:42
Post #2





Grupa: Zarejestrowani
Postów: 859
Pomógł: 177
Dołączył: 29.10.2009

Ostrzeżenie: (0%)
-----


https://jsfiddle.net/mtV62/1546/
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 28.03.2024 - 10:51