Я угловатый новичок, и я пишу приложение для ионического звука. Я закончил свое приложение и пытаюсь реорганизовать мой контроллер, избегая повторения кода.Избегайте повторения в Угловом контроллере
У меня есть этот кусок кода, который управляет моей модальностью:
angular.module('starter')
.controller('NewsCtrl', function($scope, content, $cordovaSocialSharing, $timeout, $sce, $ionicModal){
$scope.news = content;
content.getList('comments').then(function (comments) {
$scope.comments = comments;
});
$scope.addComment = function() {
};
$scope.shareAnywhere = function() {
$cordovaSocialSharing.share("Guarda questo articolo pubblicato da DDay", "Ti stanno segnalando questo articolo", content.thumbnail, "http://blog.nraboy.com");
};
$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();
};
$scope.$on('modal.shown', function() {
var footerBar;
var scroller;
var txtInput;
$timeout(function() {
footerBar = document.body.querySelector('#commentView .bar-footer');
scroller = document.body.querySelector('#commentView .scroll-content');
txtInput = angular.element(footerBar.querySelector('textarea'));
}, 0);
$scope.$on('taResize', function(e, ta) {
if (!ta) return;
var taHeight = ta[0].offsetHeight;
if (!footerBar) return;
var newFooterHeight = taHeight + 10;
newFooterHeight = (newFooterHeight > 44) ? newFooterHeight : 44;
footerBar.style.height = newFooterHeight + 'px';
scroller.style.bottom = newFooterHeight + 'px';
});
});
});
я добавил этот же код в 6 контроллеров. Есть ли способ избежать повторения?
Переместить его на службу, завод, или директивы – Tom
вы можете создать директиву для обработки модального окна – Brian
Этого Doc даст вам некоторый вклад в то, как построить и зарегистрировать службу. https://docs.angularjs.org/guide/services Вам нужно будет добавить его как зависимость в каждом из ваших контроллеров, которые будут называть ваш модальный. – steveo