当名称为字符串时如何调用函数
How to call a function when its name is a string
我有这个:
angular.module("angular-table").directive("atPagination", [
function() {
return {
restrict: "E",
scope: false,
replace: true,
template: paginationTemplate,
link: function($scope, $element, $attributes) {
//The function name
let fn = '$scope.' + $attributes.getDataFn;
console.log($scope.$eval(fn),$attributes,$attributes.getDataFn, "$eval", fn);
}
}
}
我在控制台(最后一行)中使用 $scope.$eval
调用该函数。但是函数没有被调用。
我正在尝试更改我用于 table 和分页的库。我正在尝试将一个函数从我的控制器传递到它的指令中,以便在分页中单击下一个时调用它。
我无法以常规方式传递该函数,因为 scope
是假的,我不想更改它。所以我是这样传递的
<at-pagination at-config="tableConfig"
get-data-fn="getPaginatedData()" at-list="personnelsdata">
</at-pagination>
我正在尝试使用 $eval
调用该函数,但它不起作用。我做错了什么?
app.directive("myDirective", function() {
return {
restrict: "E",
scope: false,
template: paginationTemplate,
link: function(scope, element, attrs) {
//The function name
̶l̶e̶t̶ ̶f̶n̶ ̶=̶ ̶'̶$̶s̶c̶o̶p̶e̶.̶'̶ ̶+̶ ̶ ̶$̶a̶t̶t̶r̶i̶b̶u̶t̶e̶s̶.̶g̶e̶t̶D̶a̶t̶a̶F̶n̶;̶
let fn = attrs.getDataFn;
console.log(scope.$eval(fn));
}
})
scope.$eval
方法计算 AngularJS 函数,而不是 JavaScript 函数。
有关详细信息,请参阅
我有这个:
angular.module("angular-table").directive("atPagination", [
function() {
return {
restrict: "E",
scope: false,
replace: true,
template: paginationTemplate,
link: function($scope, $element, $attributes) {
//The function name
let fn = '$scope.' + $attributes.getDataFn;
console.log($scope.$eval(fn),$attributes,$attributes.getDataFn, "$eval", fn);
}
}
}
我在控制台(最后一行)中使用 $scope.$eval
调用该函数。但是函数没有被调用。
我正在尝试更改我用于 table 和分页的库。我正在尝试将一个函数从我的控制器传递到它的指令中,以便在分页中单击下一个时调用它。
我无法以常规方式传递该函数,因为 scope
是假的,我不想更改它。所以我是这样传递的
<at-pagination at-config="tableConfig"
get-data-fn="getPaginatedData()" at-list="personnelsdata">
</at-pagination>
我正在尝试使用 $eval
调用该函数,但它不起作用。我做错了什么?
app.directive("myDirective", function() {
return {
restrict: "E",
scope: false,
template: paginationTemplate,
link: function(scope, element, attrs) {
//The function name
̶l̶e̶t̶ ̶f̶n̶ ̶=̶ ̶'̶$̶s̶c̶o̶p̶e̶.̶'̶ ̶+̶ ̶ ̶$̶a̶t̶t̶r̶i̶b̶u̶t̶e̶s̶.̶g̶e̶t̶D̶a̶t̶a̶F̶n̶;̶
let fn = attrs.getDataFn;
console.log(scope.$eval(fn));
}
})
scope.$eval
方法计算 AngularJS 函数,而不是 JavaScript 函数。
有关详细信息,请参阅