1
<li role="menuitem"><a href="#" ng-click='getData1()'>Day</a></li>
<li role="menuitem"><a href="#" ng-click='getWData2()'>Week</a></li>
<li role="menuitem"><a href="#" ng-click='getMData3()'>Month</a></li>

I have these three HTML elements defined and on click I would like to call a function in Angular. The problem is I defined three functions in angular that do the same thing but use a different parameter. How can I pass a fixed parameter into this function to pass in the string therefore helping me create one reusable function called getData():

<li role="menuitem"><a href="#" ng-click='getData('Day')'>Day</a></li>
<li role="menuitem"><a href="#" ng-click='getData('Week')'>Week</a></li>
<li role="menuitem"><a href="#" ng-click='getData('Month')'>Month</a></li>

Here is the function:

$scope.getDayData = function(day){
$scope.currentInterval = "day";

$http.get("http://localhost:8080/" + day)
.then(function(response) {


    });
5
  • 1
    can you show your functions? Commented Apr 13, 2016 at 5:04
  • That should work fine Commented Apr 13, 2016 at 5:05
  • show your getData() method Commented Apr 13, 2016 at 5:06
  • I get this error when I pass in string: angular.js:13424 Error: [$parse:ueoe] Unexpected end of expression: getDayData( Commented Apr 13, 2016 at 5:09
  • Just pass $event as argument and get the data using e.target.textContent Commented Apr 13, 2016 at 5:30

2 Answers 2

1

Change your ng-click to

<li role="menuitem"><a href="#" ng-click="getData('Day')">Day</a></li>
<li role="menuitem"><a href="#" ng-click="getData('Week')">Week</a></li>
<li role="menuitem"><a href="#" ng-click="getData('Month')">Month</a></li>

because of single inverted comma ' you have the unexpected character error.

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
  <script data-require="[email protected]" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script>
  
</head>
<body ng-app="myApp" ng-controller="myCtrl"> 
  
    <li role="menuitem"><a href="#" ng-click="getData('Day')">Day</a></li>
<li role="menuitem"><a href="#" ng-click="getData('Week')">Week</a></li>
<li role="menuitem"><a href="#" ng-click="getData('Month')">Month</a></li>
    <script>
    //module declaration
    var app = angular.module('myApp', []);
    //controller declaration
    app.controller('myCtrl',function($scope){
       
        
  $scope.getData = function(data) {

   console.log(data);
  }

    });
    </script> 
</body> 

</html>

Sign up to request clarification or add additional context in comments.

Comments

0

You should use double quotes for attributes since you are using single quote for passing the parameter:

<li role="menuitem"><a href="#" ng-click="getData('Day')">Day</a></li>
<li role="menuitem"><a href="#" ng-click="getData('Week')">Week</a></li>
<li role="menuitem"><a href="#" ng-click="getData('Month')">Month</a></li>

This may not work on some browsers. Rest of it looks good.

1 Comment

yup the single quotes were the problem. I'll accept this since it was answered first

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.