Satya
Satya

Reputation: 8881

pass a value to javascript function from angular js

I want to pass a value from Angular to a Javascript function. Angular code to render the element is:

<button class="btn btn-danger" ng-click="chatNow('{{patient.id}}');">Chat </button>

This correctly returns the HTML of button as

<button class="btn btn-danger" ng-click="chatNow('patient2');">Chat </button>

However, when I try to call the function with this param as

app.controller("VCController",function ($scope, $http,$window){
var t = $scope;
    t.chatNow = function(k) {
            console.log(k + "--"+$window.sessionStorage.getItem("docId"));
        };

});

This gives me the output on console as

{{patient.id}}--1

Any idea what am I missing? Thanks

Upvotes: 1

Views: 87

Answers (2)

user7358693
user7358693

Reputation: 523

Try this and console will thrown 2 for the id:

<!DOCTYPE html>
<html>
<head>
    <title>Hello World, AngularJS - ViralPatel.net</title>
    <script type="text/javascript"
        src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.min.js"></script>
    <script>
    angular.module('app',[])
    .controller("VCController",function ($scope, $http,$window){

        var t = $scope;
        $scope.patient = {id: 2};
        t.chatNow = function(k) {
            console.log(k + "--"+$window.sessionStorage.getItem("docId"));

            };
    });
    </script>
</head>
<body>

<div ng-app="app">
    <div ng-controller="VCController">
        <button class="btn btn-danger" ng-click="chatNow(patient.id);">Chat </button>
    </div>
</div>


</body>
</html>

Upvotes: 0

Sajeetharan
Sajeetharan

Reputation: 222722

Try without expression

<button class="btn btn-danger" ng-click="chatNow(patient.id);">Chat </button>

DEMO

var app = angular.module('DemoApp', [])
app.controller('VCController', function($scope) {
  
 var t = $scope;
 t.patient ={id:1};
  t.chatNow = function(k) {
         console.log(k + "--");
 };
 
});
<!DOCTYPE html>
<html>
<head>
  <script data-require="[email protected]" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script>
 </head>
<body ng-app="DemoApp" ng-controller="VCController">
 <button class="btn btn-danger" ng-click="chatNow(patient.id);">Chat </button>
</body>
</html>

Upvotes: 1

Related Questions