Vinay Yadav Vinay Yadav - 4 months ago 38
Javascript Question

How can I pass variable from one function to another in angularJS?

$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.

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.

Answer
$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  
     }
    });