jquery - How to pass an angularjs variable value to a javascript function? -
in code i'm attempting pass angularjs paramter function :
<div ng-app ng-controller="logincontroller"> <p><a href='javascript:display({{ user.firstname }});'>{{ user.firstname }}</a> </div> function logincontroller($scope) { $scope.user = { firstname: "foo" }; } function display(text){ alert(text) }
when "foo" clicked "foo" should displayed how pass angularjs variable value javascript function ?
fiddle : http://jsfiddle.net/lt7ap/534/
the issue value, "foo"
, being written out part of code. in case, appearing variable doesn't exist.
display(foo) // referenceerror: foo not defined
it'll need @ least quoted understood string value.
display('foo')
you can use json
filter include necessary escape sequences.
<a href='javascript:display({{ user.firstname|json }});'>{{ user.firstname }}</a>
or, if attach function $scope
, can reference , value without {{...}}
or filters in ng-click
:
function logincontroller($scope) { $scope.user = { firstname: "foo" }; $scope.display = function (text) { alert(text); }; // or keep global function , reference on $scope // $scope.display = display; }
<a ng-click="display(user.firstname)">{{user.firstname}}</a>
Comments
Post a Comment