我想添加函數到一個JS對象將被用作一個單身服務。使用Javascript對象角服務
angular
.module('app.steps')
.factory('createStepsService', createStepsService);
createStepsService.$inject = [];
/* @ngInject */
function createStepsService() {
var steps;
var service = {
newSteps: function (current_step, total_steps) {
if (!steps) {
return new Steps(current_step, total_steps);
}
}
};
return service;
function Steps(current_step, total_steps) {
this.c_step = current_step;
this.t_step = total_steps;
}
Steps.prototype = {
addSteps: function (num) {
this.c_step += num;
},
setLastStep: function() {
this.lastStep = this.c_step = this.t_step;
}
};
}
當我運行從控制器這一行,我不能夠訪問 addSteps/setLastStep方法。
vm.createStepsService = createStepsService.newSteps(1, 3);
爲什麼我沒有看到這些方法?他們是否創造了?
謝謝。
工程很棒。謝謝! – badigard
@badigard很高興幫助!瞭解變量提升和功能提升工作如何值得了解。這將幫助您編寫更好的代碼。 –