0

How to use a function assigned variable in ng-click?? I have tried three ways below, but that's also not working.

Could you please let me know is it possible or not??? if yes, then how?

var app = angular.module("app", []);
app.controller("name", function($scope) {

 $scope.showname = function()
 {
 alert("Ramesh");
 }
 
 $scope.variable1 = $scope.showname;
  
 $scope.variable2 = "showname()";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="name">
<button type="button" ng-click="variable1" tabindex="10003" >Not working 1 </button>

<button type="button" ng-click="$scope[variable2]()" tabindex="10003" >Not working 2 </button>

<button type="button"  ng-click="variable2" tabindex="10003" >Not working 3 </button>

<button type="button" ng-click="showname()" tabindex="10003"> working </button>
</div>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

4 Answers4

0

You can use $scope.$eval method and invoke the function like variable2().

$eval method don't evaluate JavaScript; they evaluate just AngularJS expressions.

Also, for the below line

$scope.variable1 = $scope.showname;

You are just keeping a reference to your showname function. You have to invoke it

<button type="button" ng-click="variable1()" tabindex="10003" >Not working 1 </button>

var app = angular.module("app", []);
app.controller("name", function($scope) {

 $scope.showname = function()
 {
 alert("Ramesh");
 }
 
  $scope.variable1 = $scope.showname;
  
  $scope.variable2 = $scope.$eval("showname");
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="name">
<button type="button" ng-click="variable1()" tabindex="10003" >Not working 1 </button>

<button type="button" ng-click="variable2()" tabindex="10003" >Not working 2 </button>

<button type="button"  ng-click="variable2()" tabindex="10003" >Not working 3 </button>

<button type="button" ng-click="showname()" tabindex="10003"> working </button>
</div>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

Try:

<button type="button" ng-click="variable1()" tabindex="10003" >Now it'll work 1 </button>

Working plunkr

Shashank Vivek
  • 16,888
  • 8
  • 62
  • 104
0

$scope.showname has function reference. So just assign it to your variable like

$scope.variable1 = $scope.showname

And then call in ng-click like valiable1()

var app = angular.module("app", []);
app.controller("name", function($scope) {

 $scope.showname = function()
 {
 alert("Ramesh");
 }
 
  $scope.variable1 = $scope.showname;
  
  $scope.variable2 =  $scope.showname;
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="name">
<button type="button" ng-click="variable1()" tabindex="10003" >Not working 1 </button>

<button type="button" ng-click="variable2()" tabindex="10003" >Not working 2 </button>

<button type="button"  ng-click="variable1()" tabindex="10003" >Not working 3 </button>

<button type="button" ng-click="showname()" tabindex="10003"> working </button>
</div>
Muhammad Usman
  • 10,039
  • 22
  • 39
0

Instead of using $scope, use the this keyword:

Erroneous

<button type="button" ng-click="$scope[variable2]()" tabindex="10003" >
     Not working 2 
</button>

Instead use:

<button type="button" ng-click="this[variable2]()" tabindex="10003" >
     working
</button>

It is possible to access the context object using the identifier this.

For more information, see AngularJS Developer Guide - Expression context.

georgeawg
  • 48,608
  • 13
  • 72
  • 95