मेरे ऐप में मेरे पास लगभग लगभग समान नियंत्रक हैं। बहुत सारे काम समान हैं, इसलिए मैं उन्हें प्रोटोटाइप करना चाहता हूं।कोणीय में 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
डालने की कोशिश की है इस नियंत्रक # 1 है एक कारखाना:
smfactory.js
angular.module('c2gyoApp')
.factory('smfactory', function() {
return {
getHours: function(minutes) {
var duration = moment.duration(minutes, 'm');
return Math.ceil(duration.asHours() % 24);
}
};
});
मैं c2gcontroller.js में smfactory
इंजेक्शन गए
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
अब कुछ भी वापस नहीं करता है।
मैंने अंगुलरजेएस सेवाओं, कारखानों, प्रदाताओं के बारे में बहुत कुछ पढ़ा और देखा है, लेकिन मुझे नहीं पता कि यहां से कहां जाना है। c2gcontroller.js
और c2gbcontroller.js
प्रोटोटाइप का उचित तरीका क्या होगा?
आपके गहन उत्तर के लिए बहुत बहुत धन्यवाद!मैंने उपयोगकर्ता 2264997 उत्तर के साथ जाने का फैसला किया क्योंकि यह यमन नामकरण योजना को तोड़ नहीं देता है। – mles