Hello everyone. I’m learning Ionic and Angular and I’m creating my first app.
Everything is ok but I want to reduce some code duplication and I’m refactoring the app.
I have basically three type of contents: news, video and gallery.
I’ integrating a bar with some options under every content, and one of this option is to show comment in a modal.
I have in my news controller this code:
angular.module('starter')
.controller('NewsCtrl', function($scope, content, $cordovaSocialSharing, $sce, $ionicModal){
$scope.news = content;
content.getList('comments').then(function (comments) {
$scope.comments = comments;
});
$ionicModal.fromTemplateUrl('templates/comments.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.showComment = function() {
$scope.modal.show();
};
// Triggered in the login modal to close it
$scope.closeComment = function() {
$scope.modal.hide();
};
});
For Gallery is the same
angular.module('starter')
.controller('NewsCtrl', function($scope, gallery, $cordovaSocialSharing, $sce, $ionicModal){
$scope.gallery = gallery;
gallery.getList('comments').then(function (comments) {
$scope.comments = comments;
});
$ionicModal.fromTemplateUrl('templates/comments.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function(modal) {
$scope.modal = modal;
});
$scope.showComment = function() {
$scope.modal.show();
};
// Triggered in the login modal to close it
$scope.closeComment = function() {
$scope.modal.hide();
};
});
As the same is for video. How can I avoid this duplication? The template is always the same, the modal is the same. The only thing that change is the call to the api (I’m using Restangular). One time I call gallery.getList
, another time video.getList
and again content.GetList
Any hint?