$scope变量从一个函数传递到另一个函数

$scope variable passing from one function to another

本文关键字:函数 一个 另一个 变量 scope      更新时间:2024-05-03

我的角度控制器的功能看起来像:

$scope.selectinteresteduser= function(Id){
   $scope.selecteduserid=Id;
}
$scope.sendMessageToEmployeer = function($scope.selecteduserid) {
   alert($scope.selecteduserid);
}

我想将$scope.selecteduserid的值传递给sendMessageToEmployer函数。怎么做?

您需要通过如下定义和调用函数,将id作为参数传递给函数:

$scope.sendMessageToEmployeer = function(id) {
   alert(id);
}
$scope.sendMessageToEmployeer($scope.selecteduserid);

无需将其作为参数传递。selecteduserid已可用于控制器中的所有功能。

$scope.sendMessageToEmployeer = function() {
   alert($scope.selecteduserid);
}
// execute function
$scope.sendMessageToEmployeer();

如果您需要从模板调用函数,请这样做;

$scope.sendMessageToEmployeer = function(id) {
   alert(id);
}

基于模板的

<button type="button" ng-click="sendMessageToEmployeer(selecteduserid)"></button>

如果你想学习,请阅读Angular文档,特别是Angular Concept。@https://docs.angularjs.org/guide/concepts.

使用$scope指定您可以在控制器中全局访问其元素。您可以直接作为$scope.selecteduserid访问它无需将其作为参数传递。请提高基础知识。