В моем приложении у меня есть два почти одинаковых контроллера. Множество функций одно и то же, поэтому я хотел бы прототипировать их. Это контроллер # 1:Как сделать прототип из 2 идентичных контроллеров в угловых?
c2gcontroller.js
angular.module('c2gyoApp')
.controller('C2gCtrl', function($scope) {
// some unique stuff
$scope.feeDay = 59;
...
// the identical functions
$scope.getMinutes = function(minutes) {
var duration = moment.duration(minutes, 'm');
return duration.minutes();
};
...
});
и контроллер # 2:
c2gbcontroller.js
angular.module('c2gyoApp')
.controller('C2gbCtrl', function($scope) {
// some unique stuff
$scope.feeDay = 89;
...
// the identical functions
$scope.getMinutes = function(minutes) {
var duration = moment.duration(minutes, 'm');
return duration.minutes();
};
...
});
Я попытался положить $scope.getMinutes
в завод:
smfactory.js
angular.module('c2gyoApp')
.factory('smfactory', function() {
return {
getHours: function(minutes) {
var duration = moment.duration(minutes, 'm');
return Math.ceil(duration.asHours() % 24);
}
};
});
Я впрыскивается smfactory
в c2gcontroller.js
c2gcontroller.js (попытка # 1)
angular.module('c2gyoApp')
.controller('C2gCtrl', function($scope, smfactory) {
...
// the identical functions
$scope.getHours = smfactory.getHours(minutes);
...
});
Это дает ошибку, минут не определено
line 33 col 42 'minutes' is not defined.
Так что я пробовал:
c2gcontroller.js (попытка # 2)
angular.module('c2gyoApp')
.controller('C2gCtrl', function($scope, smfactory) {
...
// the identical functions
$scope.getMinutes = function(minutes) {
return smfactory.getHours(minutes);
};
...
});
, который не дает ошибку, но мое приложение и не стал отвечать на запросы. В принципе $scope.getMinutes
ничего не возвращает.
Я читал и смотрел много о услугах AngularJS, фабриках, поставщиках, но я не знаю, куда идти отсюда. Каким будет правильный способ прототипа c2gcontroller.js
и c2gbcontroller.js
?
Большое спасибо за ваш глубокий ответ!Я решил пойти с ответом user2264997, поскольку он не нарушает схему именования Yeoman. – mles