如何将一个函数中的变量传递给另一个函数?

时间:2018-01-24 16:44:17

标签: angularjs

使用Angular JS,我将对象属性child.parentlink.id传递给正确记录它的函数。我想将此属性传递给另一个函数。我没有在child.parentlink.id中传递ng-click="goback()",因为我的ng-repeat在此之前停止了。

这是HTML:

<div ng-app="wizardApp">
  <div ng-controller="MainCtrl">
    ...
    <div id="pagetwo">

      <div ng-repeat="child in childresults">

        <button type="button" class="btn btn-primary" style="width:351px;" ng-click="showpagetwo(child.Title, child.ID, child.ParentLink.ID)" ng-if="child.Location == null">{{child.Title}}
          <span class="tooltiptextsmall tooltip-rightsmall" ng-if="child.TooltipSize =='Small'">{{child.TooltipText}}</span>
          <span class="tooltiptextnormal tooltip-rightnormal" ng-if="child.TooltipSize =='Normal'">{{child.TooltipText}}</span>
          <span class="tooltiptextbig tooltip-rightbig" ng-if="child.TooltipSize =='Big'">{{child.TooltipText}}</span>
        </button>
      </div>

      <button type="button" class="btn btn-primary" aria-label="Go Back" style="width:168.5px; margin-right:5.25px;" ng-click="goback()">
        <span><i class="left"></i> Go back</span>
      </button>
    </div>
  </div>
</div>

这是JS:

var app = angular.module('wizardApp', []);
app.controller('MainCtrl', function($scope, $http, $q){
  $scope.showpagetwo = function(title, id, parentlink){
    console.log("Title: " + title, id, parentlink); // logs correctly here
    ...
  };

  $scope.goback = function(parentlink){
    console.log(parentlink); // not logging here
    ...
  };

});

1 个答案:

答案 0 :(得分:0)

我能够通过创建一个名为globalparentlink的全局变量来实现这一目标。

var app = angular.module('wizardApp', []);
app.controller('MainCtrl', function($scope, $http, $q){
  $scope.showpagetwo = function(title, id, parentlink){
    console.log("Title: " + title, id, parentlink);
    globalparentlink = parentlink;
    ...
  };

  $scope.goback = function(parentlink){
    console.log(globalparentlink);
    ...
  };

});
相关问题