AngularJs ng-init function With PHP parameter in WordPress

I need to pass php variable as parameter to angulaJs function. But I am not getting the WordPress parameter in Angularjs function. I am getting undefined in AngularJs function.

My code :

Read More
<div class="ind-wrapper" data-ng-controller="mainController" data-ng-init="init(<?php echo get_the_title(); ?>)">
</div>

AngularJs function:

Here I am getting title name is undefined

$scope.init = function (titlename) 
    {
        alert(titlename)
    }

Please help!!

Related posts

1 comment

  1. You need to encode echoed variable properly, for example using json_encode function which will ensure that your title has proper quotes:

    data-ng-init="init(<?php echo json_encode(get_the_title()); ?>)"
    

    If you know that the title doesn’t contain special characters or other quotes you can also simply do this:

    data-ng-init="init('<?php echo get_the_title(); ?>')"
    

Comments are closed.