如何在angularJS中将变量从一个函数传递到另一个函数?

时间:2022-04-20 20:56:38
$scope.openModal = function (page, size) {
    console.log(page); // this is working

    $uibModal.open({
        animation: true,
        templateUrl: 'app/pages/servers/newRole.html',
        size: size,
        resolve: {
            items: function () {
                return $scope.items;
            }
        }
     });
 };

 $scope.hello = function() {
     console.log(page); // need value of page from above function

     var btn = document.createElement("BUTTON");
     var t = document.createTextNode("ABC");

     btn.classList.add("btn-primary", 'btn-xs', 'btn');
     btn.appendChild(t);

     document.getElementById('Id1').appendChild(btn);
 }

I have tried using a global variable and assigning the page to this but it is saying undefined.

我尝试过使用全局变量,并将页面分配给它,但它说的是未定义的。

I can access this variable by calling hello(page) inside openModal, but that won't work as it will call hello() when it's not needed.

我可以通过在openModal中调用hello(page)来访问这个变量,但这并不起作用,因为在不需要hello()时它会调用hello()。

I have two buttons and calling openModal function on click of 'btn1', and passing page parameter on this button and then there is another button inside that modal 'btn2', calling hello() on click of btn2.

我有两个按钮,在单击“btn1”时调用openModal function,在这个按钮上传递页面参数,然后在模式“btn2”中有另一个按钮,在单击btn2时调用hello()。

2 个解决方案

#1


0  

$scope.openModal = function (page, size) {
    console.log(page); // this is working
    $uibModal.open({
        animation: true,
        templateUrl: 'app/pages/servers/newRole.html',
        controller: ModalInstanceCtrl, //resolved items can be use in this controller
        size: size,
        resolve: {
            items: function () {
                return page; // this will return value
            }
        }
     });
 };
    angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {
     $scope.hello = function() {
         console.log(items); // Here you can get value  
     }
    });

#2


2  

Tl;Dr : use your scope, or use a service.

使用您的范围或服务。

If those methods are in the same controller:

如果这些方法在同一控制器中:

You should just be able to pass it through a variable:

你应该能够把它传递给一个变量:

  • Initialize it : $scope.data = {};
  • 初始化:美元范围。数据= { };
  • Push some data in it: $scope.data = anything; or $scope.data.field = anything.
  • 在其中推入一些数据:$scope。data =什么;或scope.data美元。场=。
  • Use it further in your function : $scope.data ... \\ Do anything
  • 在函数中进一步使用:$scope。数据……\ \做任何事

If they're not:

如果他们不是:

You can use an AngularJS service. When you finished your data treatment, you can save it into your service, then getting it back later. View it as a getter/setter for any data you would like. Example:

您可以使用AngularJS服务。当您完成数据处理之后,您可以将其保存到您的服务中,然后稍后再取回。将它视为您想要的任何数据的getter/setter。例子:


A dummy AngularJS service:

一个虚拟AngularJS服务:

var service = angular.module('yourService', [])
    .factory('$yourService', function () {
        var yourdata = {};

        return  {
            setData: function(data)    {
                yourdata = data;
            },

            getData: function() {
                return data;
            }
        };
    });

return service;

Then, be sure that you inject it into your controller. Into your first function, you can call your service as: $yourService.setData(anyData);

然后,确保将其注入到控制器中。在第一个函数中,可以将服务调用为:$yourService.setData(anyData);

And get back the data in your second function: $yourService.getData();

获取第二个函数中的数据:$ yourservicegetdata ();

#1


0  

$scope.openModal = function (page, size) {
    console.log(page); // this is working
    $uibModal.open({
        animation: true,
        templateUrl: 'app/pages/servers/newRole.html',
        controller: ModalInstanceCtrl, //resolved items can be use in this controller
        size: size,
        resolve: {
            items: function () {
                return page; // this will return value
            }
        }
     });
 };
    angular.module('ui.bootstrap.demo').controller('ModalInstanceCtrl', function ($scope, $uibModalInstance, items) {
     $scope.hello = function() {
         console.log(items); // Here you can get value  
     }
    });

#2


2  

Tl;Dr : use your scope, or use a service.

使用您的范围或服务。

If those methods are in the same controller:

如果这些方法在同一控制器中:

You should just be able to pass it through a variable:

你应该能够把它传递给一个变量:

  • Initialize it : $scope.data = {};
  • 初始化:美元范围。数据= { };
  • Push some data in it: $scope.data = anything; or $scope.data.field = anything.
  • 在其中推入一些数据:$scope。data =什么;或scope.data美元。场=。
  • Use it further in your function : $scope.data ... \\ Do anything
  • 在函数中进一步使用:$scope。数据……\ \做任何事

If they're not:

如果他们不是:

You can use an AngularJS service. When you finished your data treatment, you can save it into your service, then getting it back later. View it as a getter/setter for any data you would like. Example:

您可以使用AngularJS服务。当您完成数据处理之后,您可以将其保存到您的服务中,然后稍后再取回。将它视为您想要的任何数据的getter/setter。例子:


A dummy AngularJS service:

一个虚拟AngularJS服务:

var service = angular.module('yourService', [])
    .factory('$yourService', function () {
        var yourdata = {};

        return  {
            setData: function(data)    {
                yourdata = data;
            },

            getData: function() {
                return data;
            }
        };
    });

return service;

Then, be sure that you inject it into your controller. Into your first function, you can call your service as: $yourService.setData(anyData);

然后,确保将其注入到控制器中。在第一个函数中,可以将服务调用为:$yourService.setData(anyData);

And get back the data in your second function: $yourService.getData();

获取第二个函数中的数据:$ yourservicegetdata ();