Search code examples
javascriptangularjsvariablessyntax-errormarkup

passing $scope variable into html function markup with angular


I have a ng-repeat and I want to call a function in a ng-mouseover using two parameter as follow:

ng-mouseover="textlimit({{item.id}}, 900)"

If I do that the variable appears correctly in the sources, but the console outputs an angular syntax error. My function isn't working since then I added this variable.

How should I proceed?

PS: the variable is a number: 1, 2, 3, 4 etc.

Thanks


Solution

  • No need of {{}} in ng-mouseover since its an angular component,

    you can use the method directly like

    ng-mouseover="textlimit(item.id, 900)"

    instead of

    ng-mouseover="textlimit({{item.id}}, 900)"

    A sample example is shown below

    Working Demo

    html

    <div ng-app='myApp' ng-controller="ArrayController">
       <a href="#" onclick="return false;" ng-mouseover="textlimit(item.id, 900)">Download</a>
    </div>
    

    script

    var app = angular.module('myApp', []);
    app.controller('ArrayController', function ($scope) {
        $scope.item = {
            id:21
        }
        $scope.textlimit = function(id, value)
        {
            console.log('id::',id);
            console.log('value::',value);
        }
    });