[英]How do you call a function from within the factory using a string in angularjs
我有一些邏輯要封裝到AngularJS工廠中,以便可以使用angular的依賴項注入。 由於邏輯是動態的,因此我不一定事先知道可以調用什么。 我所擁有的是一個字符串,代表要調用的函數的名稱。 我知道我可以執行諸如window["someFunctionName"]()
來運行帶有字符串的函數,但是由於所有內容都封裝在工廠中,所以我不確定如何獲取對工廠的引用以調用它。 恩。 sampleFactory["someFuncitonName"]();
我發現運行該函數的唯一方法是使用eval("someFuncitonName()")
。 顯然,如果可以的話,我想避免使用eval。
這是我要執行的操作的一個示例:
'use strict';
angular.module('testApp')
.factory('testFactory', function (someData) {
// User defined code that was wrapped up in a factory
function foo() {
someData.amount = 5;
}
// End dynamic code
return {
funcRunner: function(functionName) {
testFactory[functionName]();
}
};
});
在控制器中,用戶將運行類似testFactory.funcRunner("foo");
。
是否有某種方法可以按照以下方式進行操作: testFactory[functionName]();
? 有更好的方法嗎? 謝謝你的幫助。
更新:由於注釋中的代碼是用戶定義的,因此我無法知道,也無法控制注釋中代碼的編寫方式。 我不想對用戶強加任何限制。 因此,我指望很少。
試試這種方法:
angular.module('testApp').factory('testFactory', function () {
var service = this;
this.foo = function() {
someData.amount = 5;
}
return {
funcRunner: function (functionName) {
service[functionName]();
}
};
});
function Ctrl($scope, testFactory) {
$scope.click = function () {
testFactory.funcRunner("foo");
}
}
您可以使用$ parse解析字符串並調用函數。 這是一個示例: http : //jsfiddle.net/WeirdAlfred/Lsfwtb96/
var app = angular.module('myApp', []);
//inject the $parse service into your controller, service, factory, etc.
app.controller('myController', ['$scope', '$parse', myController]);
function myController($scope, $parse) {
//setup a function on scope that will receive the
//string that you wish to execute as a function.
//Note: this function doesn't need to be on scope, it's just there so
//we can call it with button clicks. This could be any function
//on a controller, service, factory, etc.
$scope.buttonClick = function (someStringValue, useAngularParse) {
alert('The string value passed in from the button click: ' + someStringValue);
//the parse service takes a string and converts it to a function call
//the format for the command is:
//$parse(inputString)(objectOfAvailableFunctions, objectOfParams);
$parse(someStringValue)(internalFunctions);
}
//this provides a list of available functions for the
//$parse to call when it evaluates the given string.
var internalFunctions = {
catsSuckEggs: catsSuckEggs,
dogsRock: dogsRock
}
//some random function that you want to make available via a string
function catsSuckEggs() {
alert('My cat is worthless!');
}
//some other random function that you want to make available via a string
function dogsRock() {
alert('I wish my wife would let me have a dog!');
}
}
這是一些啟用markClick並傳遞字符串的快速標記。
<body ng-app="myApp">
<div ng-controller="myController">
<h2>Do you like Cats or Dogs?</h2>
<button class="btn btn-danger" ng-click="buttonClick('catsSuckEggs()')">Cats</button>
<button class="btn btn-success" ng-click="buttonClick('dogsRock()')">Dogs</button>
</div>
</body>
據我所讀,沒有辦法做到這一點。 它就是javascript的工作方式。 無法通過字符串調用foo函數。 foo函數必須是對象的一部分才能使用字符串調用它。 我只是想確認沒有任何聰明的方法可以解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.