Custom Factory in services.js
To begin, create a custom factory and replace the example URL with your own.
.factory('CustomData', function ($http, $rootScope, $stateParams) {
return {
all: function () {
return $http.get('https://customdata.json/all', { params: { user_id: $rootScope.session } })
},
get: function () {
return $http.get('https://customdata.json/getOne', { params: { user_id: $rootScope.session, chat_id: $stateParams.idchat } })
},
add: function (id) {
return $http.get('https://customdata.json/new', { params: {id:id}})
}
};
});
Controller Configuration in controllers.js
Next, set up a controller to interact with the factory and retrieve data for use in the application.
.controller('CustomCtrl', function ($scope, CustomData) {
CustomData.all().success(function (response) {
$scope.customData = response;
})
})
Binding Data to View Scope
Bind the retrieved data to the view scope so it can be displayed to the user.
Now, you can display the data in the view by accessing it through the $scope variable. Use this list to showcase the data obtained from the server with options to add or delete entries.
<ion-view view-title="Contacts">
<ion-content>
<ion-list>
<ion-item class="item-icon-right" ng-repeat="item in customData">
<h1>{{ item.username }}</h1>
<p>{{ item.friendNumber}}</p>
<i class="icon ion-chevron-left icon-accessory"></i>
<ion-option-button class="button-positive" ng-click="viewFriend(viewFriend(item.id))">View Friend</ion-option-button>
<ion-option-button class="button-assertive" ng-click="deleteFriend(remove(item.id))">Delete</ion-option-button>
</ion-item>
</ion-list>
</ion-content>
</ion-view>
Note:
The code provided is based on an adaptation of existing code. While I haven't tested this exact version, the original code functions properly. For more details or the complete code, visit my GitHub repository https://github.com/joeLloyd/Scripto5000