Getting data from json file in IONIC using AngularJS

Problem

I created my first project with an ionic structure using the ion tab template, and this is an example project: ( https://github.com/driftyco/ionic-starter-tabs ) As we see in this project, we get a list of friends and a friend’s detail from the list of arrays created in the services.js file, but I want to get this list and elements from a json file like this (example https: //friends.json ).

Question

How can I pull JSON data from a web server to my application?

+3
source share
1 answer

Factory in services.js

factory, url wit yours.

.factory('Friends', function ($http, $rootScope, $stateParams) {

  return {
    all: function () {
      return $http.get('https://friends.json/all', { params: { user_id: $rootScope.session } })
    },
    get: function () {
      return $http.get('https://friends.json/getOne', { params: { user_id: $rootScope.session, chat_id: $stateParams.idchat } })
    },
    add: function (id) {
      return $http.get('https://friends.json/new', { params: {idfriends:id}})
    }
  };
});

.js

. factory .

.controller('FirendsCtrl', function ($scope, Friends) {

  Friends.all().success(function (response) {
    $scope.friends = response;
  })
})

​​

. , .

, $scope , , , .

<ion-view view-title="Contacts">
  <ion-content>
  <ion-list>
      <ion-item class="item-icon-right" ng-repeat="data in friends">
          <h1>{{ data.username }}</h1>
          <p>{{ data.friendNumber}}</p>
          <i class="icon ion-chevron-left icon-accessory"></i>
          <ion-option-button class="button-positive" ng-click="viewFriend(viewFriend(data.idfriends))">View Friend</ion-option-button>
          <ion-option-button class="button-assertive" ng-click="deleteFriend(remove(data.idfriends))">Delete</ion-option-button>
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>

- , , . , .

, , github repo https://github.com/joeLloyd/Scripto5000

+9

Source: https://habr.com/ru/post/1623294/


All Articles