1

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

3
  • Use chatNow(patient.id) Commented Dec 31, 2016 at 5:51
  • @Tushar , where should I change? Can you please illustrate. Thanks Commented Dec 31, 2016 at 5:53
  • @Tushar , thanks a ton man. You rock!! Commented Dec 31, 2016 at 6:08

2 Answers 2

1

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>

Sign up to request clarification or add additional context in comments.

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.