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> 

http://jsfiddle.net/7z4wmnjj/


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -