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

Pass a function value inside another function (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)
}

我有 2 个不同的函数,因为值将来自 2 个不同的点击。我需要在 myThirdFunction 中获取这 2 个值。

谢谢。

快速修改您的 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]);
}