我需要将一个函数的值传递给另一个函数。像这样:
的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]);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句