1
我有一個服務函數,我試圖在控制器中調用。該服務還調用同一服務中的另一個功能。當我可以從控制器調用服務時,如果我明確傳入第二個服務,我只能使其工作。我想要做的是能夠傳遞一個調用第二個服務的字符串。它看起來更乾淨,更容易閱讀。但我無法弄清楚如何做到這一點。如何調用函數另一個函數作爲參數傳遞爲字符串
服務:
function $cmCallOut($timeout, $cmAutoscroll) {
var service = {
runCallOut: runCallOut,
unavailable: unavailable,
destroy: destroy,
create: create,
update: update
};
return service;
function runCallOut(recordId, callout) {
// Wrap in timeout to wait for DOM to load before grabbing the element
$timeout(function() {
// Get element which to perform callout animation on
var element = angular.element('[record-id=' + recordId + ']');
// Get the elements coords so we can scroll to it
var rect = element[0].getBoundingClientRect();
// Scroll to element
$cmAutoscroll.toTop(rect.top);
// Run animation
callout(element);
});
}
function unavailable(element) {
element.velocity('callout.shake');
}
function destroy(element) {
element.velocity('transition.expandOut', {duration : 350});
}
function create(element) {
element.css('display', 'none');
element
.velocity('transition.expandIn', {duration : 450, display: 'block', delay: 350})
.velocity('callout.flash');
$timeout(function() {
element.css('transform', 'none');
}, 2000);
}
function update(element) {
element.velocity('callout.flash', {duration: 1500, delay: 350});
}
}
控制器:
$cmCallOut.runCallOut(chargeId, $cmCallOut.create);
想這樣稱呼它:
$cmCallOut.runCallOut(chargeId, 'create');
關於關聯數組什麼'變種FUNC = {創建:$ cmCallOut.create}; $ cmCallOut.runCallOut(chargeId,func ['create']);' –
需要在第一次注入第二個服務並從控制器中移除該注入 – charlietfl
如果我理解正確,您要傳遞一個字符串作爲第二個參數並決定哪個函數根據該字符串調用。在這種情況下,您可以使用「開關」並添加您擁有的不同案例,並根據發送的參數 – Dola