Getting data from a json file in IONIC using AngularJS

后端 未结 1 1313
隐瞒了意图╮
隐瞒了意图╮ 2020-12-18 11:14

Problem

I created my first project with ionic framework using ionic tabs template and this is an example of the project : (https://github.com/drift

相关标签:
1条回答
  • 2020-12-18 11:49

    Factory in services.js

    Start off with a factory but replace my 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}})
        }
      };
    });
    

    Controller in controllers.js

    Then make a controller like this. This is when you instantiate the factory to get your data.

    .controller('FirendsCtrl', function ($scope, Friends) {
    
      Friends.all().success(function (response) {
        $scope.friends = response;
      })
    })
    

    Scope added to View

    This will bind the data to the scope. And there for make it available to the view.

    Finally you will be able to bring the data to the view via $scope I have created a list here of all the friends that you get from get friend with a nice side swip to add more or delete.

    <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>
    

    Disclaimer

    this is some code that I had previously written, I've modified it a bit to fit with your question. I have not ran this version exactly but my full version works fine.

    if you wish to see the full code please feel free to check out my github repo https://github.com/joeLloyd/Scripto5000

    0 讨论(0)
提交回复
热议问题