1

Allow me to begin by saying that I am a C++ developer by profession and have extremely poor javascript skills, so please forgive me if this is a basic question. What I am trying to do is take a string being generated from an Angular binding and pass it to a function in javascript. I have included an example below. Note that I am trying to pass {{x.VodName}} to the function called "MyFunction(vodName)". Thanks in advance!

<tr ng-repeat="x in names">
<td><a id="myLink" href="#" onclick="MyFunction({{ x.VodName }});">{{ x.VodName }}</a></td>
<td>{{ x.Stream }}</td>
<td>{{ x.ReplayTime }}</td>
<td>{{ x.Duration }}</td>
<td>X</td>

<script>
function MyFunction(vodName)
{
    window.alert(vodName);
}

1
  • don't use onClick , use angular ng-click along with your function being part of controller scope Commented Dec 5, 2014 at 16:18

2 Answers 2

2

In your template you can give an variable to a function by just giving it to the function. And instead of onclick you should use ng-click

<tr ng-repeat="x in names">
  <td><a id="myLink" href="#" ng-click="MyFunction(x.VodName);">{{ x.VodName }}</a></td>
  <td>{{ x.Stream }}</td>
  <td>{{ x.ReplayTime }}</td>
  <td>{{ x.Duration }}</td>
  <td>X</td>
</tr>

For you function in the controller it can be important to know the difference between var MyFunction = function(name){} and function MyFunction(name){}. For more about this, look here

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

Comments

0

try something like this, where your controller could look like:

<script>
var vapp = angular.module("vapp", []);
vapp.controller("videoController", ["$scope", function($scope){
    $scope.names = [];

    $scope.play = function(files){
      alert(files.VodName);
    };
}]);
</script>

and your html:

<table ng-controller="videoController">
    <tr ng-repeat="x in names">
      <td><a id="myLink" href="#" ng-click="play(x);">{{ x.VodName }}</a></td>
      <td>{{ x.Stream }}</td>
      <td>{{ x.ReplayTime }}</td>
      <td>{{ x.Duration }}</td>
      <td>X</td>
    </tr>
</table>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.