在另一个函数(Angular js)中传递一个函数值



我需要将一个值从一个函数传递给另一个函数。像这样:

HTML

<div>
<li ng-repeat="language in languages" ng-click="myFirstFunction(firstValue)
{{language.lang}}
</li>
</div>
<div>
<li ng-repeat="age in agess" ng-click="mySecondFunction(secondValue)
{{age.year}}
</li>
</div>

JS

$scope.myFirstFunction = function (firstValue) {
console.log(firstValue);
}
$scope.mySecondFunction = function (secondValue) {
console.log(secondValue);
}
$scope.myThirdFunction = function () {
$scope.myFirstFunction(firstValue) // I need to import this value into this myThirdFunction()
$scope.mySecondFunction(secondValue) // I need to import this value into this myThirdFunction()
// console.log(firstValue);
// console.log(secondValue)
}

我有两个不同的函数,因为值将从两次不同的点击中获得。我需要在myThirdFunction中取这两个值。

谢谢。

对JS代码进行快速修改就可以了。

let values = [];//place to store values
$scope.myFirstFunction = function (firstValue) {
values[0] = firstValue;//stores first value
console.log(firstValue);
}
$scope.mySecondFunction = function (secondValue) {
values[1] = secondValue;//stores second value
console.log(secondValue);
}
$scope.myThirdFunction = function () {
// you can use the values here
// console.log(values[0]);
// console.log(values[1]);
}

最新更新