将变量从一个函数传递给另一个函数

时间:2017-07-10 05:45:28

标签: javascript angularjs

我有这样的选择:

<select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" ng-model="selected" ng-options="item.Nombre for item in Catalogos "></select>

从那里我得到selected value这样的函数:

$scope.filtro = function(selected) {
            $scope.selectedID = selected.ID;
        }

Id得到selected value,但现在我想将此parameter发送给另一个函数,如:

$scope.insertar = function(selected) {
                $scope.selectedID = selected.ID;
                if ($scope.catalogoid != null) {...

selected总是未定义

注意:当我提交按钮

时会触发$scope.insertar
<button type="submit" class="btn blue" ng-click="insertar();">Aceptar</button>

非常感谢帮助!此致

1 个答案:

答案 0 :(得分:3)

您应该只在insertar函数中使用范围变量,而不是重新分配它。像这样:

$scope.insertar = function() {
    var selectedId = $scope.selectedID; 
    if ($scope.catalogoid != null) {...